-1
我以前工作的网络刮刀。但是,它是专门从刮网站最近已改为使用HTTPS,使得下面不再工作相关的代码片段。Python的 - 从HTTPS解析HTML使用LXML网址 - urllib2.urlopen解决办法是行不通的
from lxml import html
url = 'http://www.blahblah.com'
good_data = False
while good_data ==False:
try:
data = html.fromstring(html.tostring(html.parse(url)))
good_data=True
except Exception:
good_data=False
print 'scraping failed'
winsound.Beep(800,1000)
time.sleep(pause)
以前的数据将被成功解析为'数据',我现在从lxml中得到一个IOError。
我做了一些阅读 - 在这里尤其是:error with parse function in lxml - 我试图实现使用的urllib2将给出解决方案如下:
from lxml import html
from urllib import urlopen
url = 'http://www.blahblah.com'
good_data = False
while good_data ==False:
try:
data = html.fromstring(html.tostring(html.parse(urlopen(url))))
good_data=True
except Exception:
good_data=False
print 'scraping failed'
winsound.Beep(800,1000)
time.sleep(pause)
,但现在我回来了错误
"TypeError: expected string or buffer".
这会发生是否我从http或https://www.blahblah.com刮。
无论是对原来的问题或修复原始问题的修复程序的修复,将不胜感激。
您是否尝试过'请求? – alecxe
刚刚尝试过。有没有r = requests.get(url) - 我认为是正确的。它不会把我的任何错误,但实际上并没有,似乎给了我什么,即r.text,r._content都为空 – cmcsorley17
得到它 - 不得不为这里欺骗的Chrome用户代理HTTP://计算器。 com/questions/25491872/python-request-geturl-returns-empty-content让它起作用。 – cmcsorley17