【发布时间】: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