【发布时间】:2018-02-04 23:50:26
【问题描述】:
我正在尝试使用 URL 阅读 Twitter 提要。昨天我能够使用该代码提取一些 80K 推文,并且由于我的机器上的一些更新,我的 Mac 终端在 python 代码完成之前停止响应。
今天相同的代码没有返回任何 json 数据。它给我带来了空洞的结果。如果我在浏览器中输入相同的 URL,我可以获得一个包含完整数据的 json 文件。
这是我的代码: 方法一:
try:
urllib.request.urlcleanup()
response = urllib.request.urlopen(url)
print('URL to used: ', url)
testURL = response.geturl()
print('URL you used: ', testURL)
jsonResponse = response.read()
jsonResponse = urllib.request.urlopen(url).read()
这个打印出来的:
URL to used: https://twitter.com/i/search/timeline?f=tweets&q=%20since%3A2017-08-14%20until%3A2017-08-15%20USA&src=typd&max_position=
URL you used: https://twitter.com/i/search/timeline?f=tweets&q=%20since%3A2017-08-14%20until%3A2017-08-15%20USA&src=typd&max_position=
json: {'items_html': '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n', 'focused_refresh_interval': 30000, 'has_more_items': False, 'min_position': 'TWEET--', 'new_latent_count': 0}
****方法二:****
try:
request = urllib.request.Request(url, headers=headers)
except:
print("Thats the problem here:")
try:
response = urllib.request.urlopen(request)
except:
print("Exception while fetching response")
testURL = response.geturl()
print('URL you used: ', testURL)
try:
jsonResponse = response.read()
except:
print("Exception while reading response")
这两种情况的结果相同。
请帮助。
【问题讨论】:
-
我得到了和你一样的回应。
-
这很奇怪……有时我明白,有时却没有
-
好吧,我也没有在浏览器中得到合理的响应。