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。
我对requests和Python都很陌生,也刚刚开始接触stackoverflow。
1 个回答
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)