我是卡内基梅隆大学的新生,他完全迷失在第一个学期的项目中,我非常感谢您的帮助:)
我正在编写一个抓取工具,有时请求没有响应。它不返回任何东西;它甚至不返回错误。这个问题使我的抓取工具卡在一个 URL 上,而不是意识到它被卡住并继续前进。这是代码:
def extractHTML(url):
startTime = time.time()
headers = requests.utils.default_headers()
headers.update(
{'User-Agent':
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',})
paper1Link = requests.get(url,headers=headers)
papaer1Content=BeautifulSoup(paper1Link.content,"lxml")
return str(papaer1Content)
- 如何让 python 识别出我被卡住并中断几秒钟?
- 此网站:http://www.apa.org/即使我更改 header 以使请求看起来像普通的单独请求,也不会授予我访问权限。我如何才能从中获取请求?
最佳答案
requests
documentation有一个section called "Timeouts".也许你应该从这里开始。
paper1Link = requests.get(url,headers=headers, timeout=0.4)
关于python - 抓取时收到请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43622686/