【问题标题】:My python code output wrong html data我的python代码输出错误的html数据
【发布时间】:2015-04-19 21:24:26
【问题描述】:

我将此代码 sn-p 作为 python 代码的一部分来抓取特定网站(请参见下面的代码)。但令我惊讶的是,输出代码不是 html。我正在使用 python 3.4

   import urllib.request as ur
   user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
   headers = { 'User-Agent' : user_agent }

   s = ur.urlopen('http://www.nairaland.com')
   pl = s.read()
   print(pl) 

这段代码的输出是:

b''

而不是预期的 html 代码。请指导我使此代码正常工作。我需要代码的另一部分中的 html 代码。提前致谢。

【问题讨论】:

    标签: python html web-crawler


    【解决方案1】:

    出色的 requests 库返回正确的 HTML:

    import requests
    s = requests.get('http://www.nairaland.com')
    pl = s.text
    print(pl)
    

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 2016-06-27
      • 2020-09-03
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多