Skip to main content
 首页 » 编程设计

python请求获取网页失败

2025年05月04日13over140

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

请您参考如下方法:

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