我想读通过URL Twitter的饲料。昨天我能拉使用的代码和一些80K鸣叫,由于我的机器上的一些更新,我的Mac终端停止完成Python代码之前响应。urllib.request.urlopen是表现奇怪。第二天不返回数据。为什么?
今天,同样的代码不会返回任何JSON数据。这是扔给我空的结果。虽然如果我在浏览器中输入相同的URL,我可以得到一个充满数据的json文件。
这是我的代码: 方法1:
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}
****方法2:****
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")
在这两种情况下结果相同。
请帮忙。
我得到同样的响应,你怎么做。 – elena
奇怪的是正确的。有时我得到它,有时不 –
嘛,我不要在浏览器中得到合理的回应要么。 – elena