我正在抓取几个URL以在其源代码中查找特定关键字。然而,虽然爬行一半的网站,我的蜘蛛突然停止由于像404或503 HTTP错误如何在使用python 2.7抓取URL时忽略HTTP错误
我的履带:
import urllib2
keyword = ['viewport']
with open('listofURLs.csv') as f:
for line in f:
strdomain = line.strip()
if strdomain:
req = urllib2.Request(strdomain.strip())
response = urllib2.urlopen(req)
html_content = response.read()
for searchstring in keyword:
if searchstring.lower() in str(html_content).lower():
print (strdomain, keyword, 'found')
f.close()
我要补充什么代码,忽略与HTTP错误和出租不良网址爬行器继续cra??
您可以在响应对象上调用getCode()并使用条件来检查200状态。 – tobassist
@tobassist你能告诉我我特别需要哪些代码行吗? – jakeT888