【问题标题】:Python requests fails to get webpagesPython 请求获取网页失败
【发布时间】:2013-02-08 19:18:13
【问题描述】:

我正在使用 Python3 和包 requests 来获取 HTML 数据。

我已经尝试过运行这条线

r = requests.get('https://github.com/timeline.json')

,这是他们教程中的示例,但无济于事。但是,当我运行

request = requests.get('http://www.math.ksu.edu/events/grad_conf_2013/')

它工作正常。我收到诸如

之类的错误
AttributeError: 'MockRequest' object has no attribute 'unverifiable' 
Error in sys.excepthook:

我认为这些错误与我尝试获取的网页类型有关,因为正在运行的 html 页面只是我编写的基本 html。

我对请求和 Python 很陌生。我也是 stackoverflow 的新手。

【问题讨论】:

  • 看来已经过时了
  • 无论如何,看起来像是您特定版本的 Python 中的一个错误,可能与 https 协议处理有关。对我有用 python 3.2.33.3.0
  • 我正在使用全新安装的 3.3,知道如何处理这个问题吗?
  • 不确定发生了什么变化,但重新登录后,thins 工作正常...

标签: python python-3.x python-requests


【解决方案1】:

作为一个小例子,这是我开发的一个小工具,用于从网站获取数据,在本例中为 IP 并显示它:

# Import the requests module
# TODO: Make sure to install it first
import requests

# Get the raw information from the website
r = requests.get('http://whatismyipaddress.com')
raw_page_source_list = r.text
text = ''

# Join the whole list into a single string in order
# to simplify things
text = text.join(raw_page_source_list)

# Get the exact starting position of the IP address string
ip_text_pos = text.find('IP Information') + 62

# Now extract the IP address and store it
ip_address = text[ip_text_pos : ip_text_pos + 12]

# print 'Your IP address is: %s' % ip_address
#           or, for Python 3 ...            #
# print('Your IP address is: %s' % ip_address)

【讨论】:

    猜你喜欢
    • 2016-03-02
    • 2017-03-10
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2021-04-25
    • 2022-09-24
    相关资源
    最近更新 更多