検索

ホームページ  >  に質問  >  本文

python - 求高手,使用urllib2请求网页内容时请求成功却返回空的值?

这是出现类似问题的一个网页http://detail.zol.com.cn/inde...

测试代码
import urllib2
url = 'http://detail.zol.com.cn/inde...'
response = None
try:
response = urllib2.urlopen(url,timeout=5)
html = response.read()
print html
print "hehe"
except urllib2.URLError as e:
if hasattr(e, 'code'):

print 'Error code:',e.code

elif hasattr(e, 'reason'):

print 'Reason:',e.reason

finally:
if response:

response.close()

运行结果:C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled/data02
hehe

Process finished with exit code 0

这段代码运行后也是空值
page = urllib2.Request(url)
page.add_header('Referer', url)
page.add_header('User-Agent', "Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0")
r = urllib2.urlopen(page,timeout=5.0)
html = r.read()
soup = BeautifulSoup(html, 'lxml')

PHP中文网PHP中文网2889日前371

全員に返信(2)返信します

  • PHP中文网

    PHP中文网2017-04-18 09:16:31

    暴力的になってクッキーを持ってきてください

    リーリー

    返事
    0
  • ringa_lee

    ringa_lee2017-04-18 09:16:31

    このページにアクセスすると、ブラウザーの Cookie も空になっていることがわかり、その Cookie が暗号化され、js で設定されていることがわかりました。うまくいかない場合は、Chrome を使ってみましょう。

    返事
    0
  • キャンセル返事