Python请求无法获取网页

时间:2013-02-08 19:18:14

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

我正在使用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的新手。

1 个答案:

答案 0 :(得分:0)

作为一个小例子,这是我开发的一个小工具,用于从网站获取数据,在这种情况下是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)