我正在尝试使用Python 3.x中的
请求会被重定向(通过HTTP状态代码301)到浏览器中的http://www.finanzen.net/etf/ishares_core_dax%AE_ucits_etf_de(其中包含带有字符0xAE ®的URL)。使用重定向后的URL和
当尝试使用Python 2.7获取搜索字符串URL时,一切正常并且我会收到重定向响应,但使用Python 3.x时,我会收到以下错误:
测试此功能的代码片段如下:
requests.get()
获取以下URL:http://www.finanzen.net/suchergebnis.asp?strSuchString=DE0005933931(该URL由具有搜索字符串DE0005933931
的基本URL组成)。请求会被重定向(通过HTTP状态代码301)到浏览器中的http://www.finanzen.net/etf/ishares_core_dax%AE_ucits_etf_de(其中包含带有字符0xAE ®的URL)。使用重定向后的URL和
requests.get()
一起使用也可以正常工作。当尝试使用Python 2.7获取搜索字符串URL时,一切正常并且我会收到重定向响应,但使用Python 3.x时,我会收到以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 21: invalid start byte
测试此功能的代码片段如下:
import requests
url_1 = 'http://www.finanzen.net/suchergebnis.asp?strSuchString=LU0274208692'
# redirected to http://www.finanzen.net/etf/db_x-trackers_msci_world_index_ucits_etf_1c
url_2 = 'http://www.finanzen.net/suchergebnis.asp?strSuchString=DE0005933931'
# redirected to http://www.finanzen.net/etf/ishares_core_dax%AE_ucits_etf_de
print(requests.get(url_1).status_code) # working
print(requests.get(url_2).status_code) # error with Python 3.x
更多信息:
- 我正在使用Python 3.6.3在Windows 7上工作,
requests.__version__ = '2.18.4'
,但我使用其他Python版本(3.4、3.5)时也遇到了同样的错误。 - 使用其他搜索字符串,Python 3.x也可以正常工作,例如:http://www.finanzen.net/suchergebnis.asp?strSuchString=LU0274208692
- 有趣的是,即使在https://www.hurl.it上尝试获取上述URL时,我仍然会收到一个“内部服务器错误”。也许这不是Python的问题。
你有任何想法,为什么Python 2.7可以工作,而Python 3.x却不能?我该怎么办?
requests.get("http://www.finanzen.net/etf/ishares_core_dax%AE_ucits_etf_de")
返回了成功的响应信息 (<Response [200]>
),我刚在 Python 3.6.3 上进行了测试,使用的是 requests 版本 2.18.4。 - Tomalak