1

I'm trying to scrape the page 'https://zhuanlan.zhihu.com/wangzhenotes' with Scrapy.

I run this command

scrapy shell 'https://zhuanlan.zhihu.com/wangzhenotes'

and got

DEBUG: Crawled (400) <GET https://zhuanlan.zhihu.com/wangzhenotes> (referer: None)

I guess I'm encountering some kind of anti-Scraping. How do I know what techniques the site is using?

Here is the full logging

(base) $ scrapy shell 'https://zhuanlan.zhihu.com/wangzhenotes'
2020-07-01 09:46:03 [scrapy.utils.log] INFO: Scrapy 2.1.0 started (bot: scrapybot)
2020-07-01 09:46:03 [scrapy.utils.log] INFO: Versions: lxml 4.5.1.0, libxml2 2.9.10, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 20.3.0, Python 3.7.7 (default, May  6 2020, 04:59:01) - [Clang 4.0.1 (tags/RELEASE_401/final)], pyOpenSSL 19.1.0 (OpenSSL 1.1.1g  21 Apr 2020), cryptography 2.9.2, Platform Darwin-17.7.0-x86_64-i386-64bit
2020-07-01 09:46:03 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
2020-07-01 09:46:03 [scrapy.crawler] INFO: Overridden settings:
{'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter',
 'LOGSTATS_INTERVAL': 0}
2020-07-01 09:46:03 [scrapy.extensions.telnet] INFO: Telnet Password: 32acb90e56ac4d67
2020-07-01 09:46:03 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.memusage.MemoryUsage']
2020-07-01 09:46:03 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2020-07-01 09:46:03 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2020-07-01 09:46:03 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2020-07-01 09:46:03 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6024
2020-07-01 09:46:03 [scrapy.core.engine] INFO: Spider opened
2020-07-01 09:46:10 [scrapy.core.engine] DEBUG: Crawled (400) <GET https://zhuanlan.zhihu.com/wangzhenotes> (referer: None)
[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    <scrapy.crawler.Crawler object at 0x10ba0a090>
[s]   item       {}
[s]   request    <GET https://zhuanlan.zhihu.com/wangzhenotes>
[s]   response   <400 https://zhuanlan.zhihu.com/wangzhenotes>
[s]   settings   <scrapy.settings.Settings object at 0x10ba0a2d0>
[s]   spider     <DefaultSpider 'default' at 0x10bf4e210>
[s] Useful shortcuts:
[s]   fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s]   fetch(req)                  Fetch a scrapy.Request and update local objects 
[s]   shelp()           Shell help (print this help)
[s]   view(response)    View response in a browser

After adding this to settings.py

DEFAULT_REQUEST_HEADERS = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}

The logging turned to

2020-07-01 11:43:37 [scrapy.core.engine] DEBUG: Crawled (404) <GET https://zhuanlan.zhihu.com/robots.txt> (referer: None)

...

2020-07-01 11:43:37 [protego] DEBUG: Rule at line 19 without any user agent to enforce it on.

...

2020-07-01 11:43:38 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://zhuanlan.zhihu.com/wangzhenotes> (referer: None)
JJJohn
  • 915
  • 8
  • 26

1 Answers1

1

Add this middlewire to the middleware.py file -

class CustomMiddleware(object):
    def process_request(self, request, spider):
        request.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"

then replace all the previous middlewares with the new one, like this.

DOWNLOADER_MIDDLEWARES = {
    'projectname.middlewares.CustomMiddleware': 543,
}

no longer need this -

DEFAULT_REQUEST_HEADERS = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
pritam samanta
  • 434
  • 4
  • 10
  • Thank you. I believe we're close to the solution. Now the logging turns to "Crawled (404)" and "Crawled (200)". I just updated the OP. – JJJohn Jul 01 '20 at 03:48
  • If you still having issue uo can use a 3rd party library: `pip install scrapy-user-agents` and then add this miidlewire `DOWNLOADER_MIDDLEWARES = { 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None, 'scrapy_user_agents.middlewares.RandomUserAgentMiddleware': 400, }` – pritam samanta Jul 01 '20 at 03:56
  • Thank you. Now I get `2020-07-01 12:02:20 [scrapy_user_agents.user_agent_picker] WARNING: [UnsupportedBrowserType] Family: Android; 2020-07-01 12:02:20 [scrapy_user_agents.user_agent_picker] WARNING: [UnsupportedDeviceType] Family: Other, Brand: None, Model: None` – JJJohn Jul 01 '20 at 04:03
  • ok..this is normal logging outpt while using this library..what about the status code? – pritam samanta Jul 01 '20 at 04:06
  • the same as before installing scrapy-user-agents, 404 first, and then 200 – JJJohn Jul 01 '20 at 04:08
  • I really appreciate your help. I guess [the warning is not the one blocking the crawler](https://stackoverflow.com/q/62670277/12214867). Any other things I could try? – JJJohn Jul 01 '20 at 05:32
  • nothing to worry about the warning..it happens when the `RandomUserAgentMiddleware` tries different middlewares from a user agents list until it finds the correct one. – pritam samanta Jul 01 '20 at 05:43
  • One last thing you can do is to create your own custom middle wire – pritam samanta Jul 01 '20 at 05:43
  • Thank you. I'd love to do that. How do I find a clue I could follow to create my own custom middleware. Just with "404", I really don't know where should I start? – JJJohn Jul 01 '20 at 06:17
  • Hey @JJJohn, I have made few changes to the answer, it should work fine..i've just tested this code.. it runs smoothly for me.. – pritam samanta Jul 01 '20 at 14:55
  • Thank the crawler is still being blocked. However, I realized that the original question has been answered. Shall we discuss it on another post. – JJJohn Jul 02 '20 at 02:43