【问题标题】:Python 3 Website detects scraper when using User-Agent spoofingPython 3 网站在使用用户代理欺骗时检测到刮板
【发布时间】:2017-04-15 03:18:57
【问题描述】:

我正在尝试使用 urllib 从 Indeed.com 上抓取一些信息。有时,工作链接会被重定向到招聘公司的网页。发生这种情况时,Indeed 会抛出一些关于使用不兼容的浏览器或设备的 html,而不是继续访问重定向的页面。环顾四周后,我发现在大多数情况下,将 urllib 的用户代理伪装成浏览器就足以解决这个问题,但这里似乎并非如此。

除了欺骗用户代理之外,有什么建议吗?是否有可能 Indeed 能够意识到 User-Agent 被欺骗,并且没有办法解决这个问题?

下面是代码示例:

import urllib
from fake_useragent import UserAgent
from http.cookiejar import CookieJar

ua = UserAgent()

website = 'http://www.indeed.com/rc/clk?jk=0fd52fac51427150&fccid=7f79c79993ec7e60'
req = urllib.request.Request(website)
cj = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-Agent', ua.chrome)]
response = opener.open(req)

print(response.read().decode('utf-8'))

感谢您的帮助!

【问题讨论】:

  • 您可以尝试以无头方法(使用 Xvfb)使用 Selenium,这将通过实际使用浏览器(但以编程方式)“避免”检测到浏览器。

标签: python python-3.x web-scraping urllib user-agent


【解决方案1】:

这个标题通常有效:

HDR = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}

另一种选择是使用requests 包。

【讨论】:

    猜你喜欢
    • 2012-02-20
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多