发送不同的json请求,但在Python中获得类似的结果

时间:2016-11-23 08:43:29

标签: python json python-2.7 request infinite-scroll

我正在尝试发送json请求来抓取this link中的无限滚动元素。虽然,我知道有些参数不是必需的,但可以肯定的是,我定义了与发送到服务器完全相同的参数:我的参数和代码是

import requests
import json
parameters1 = {'ticker':'XOM', 'countryCode':'US',
           'dateTime':'12%3A38+p.m.+Oct.+24%2C+2016', 'docId':'',
           'docType':'806','sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
           'messageNumber':'8541','count':'10',
          'channelName':'%2Fnews%2Flatest%2Fcompany%2Fus%2Fxom', 'topic':'',
           '_':'1479888927416' }



parameters2 = {'ticker':'XOM', 'countryCode':'US',
           'dateTime':'12%3A38+p.m.+Oct.+24%2C+2016','docId':'',
           'docType':'806'  ,'sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2', 
           'messageNumber':'8525','count':'10',
           'channelName':'%2Fnews%2Flatest%2Fcompany%2Fus%2Fxom', 'topic':'',
           '_':'1479888927417' }


firstUrl = "http://www.marketwatch.com/news/headline/getheadlines"
html1 = requests.get(firstUrl, params = parameters1)
result1 = (json.loads(html1.text))

html2 = requests.get(firstUrl, params = parameters2)
result2 = (json.loads(html2.text))

我检查它们是否相同:

if(result2 == result1):
     print(True)

答案永远是真的。我改变了许多参数,但它没有用。我的代码或工作有什么问题?

1 个答案:

答案 0 :(得分:1)

您的问题是,您正在发送JSON但使用编码字符串。我应该使用%2F而不是/空格而不是+一个%3A,而不是:,而不是import requests import json parameters1 = {'ticker':'XOM', 'countryCode':'US', 'dateTime':'12:38 p.m. Oct., 2016', 'docId':'', 'docType':'806','sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2', 'messageNumber':'8541','count':'10', 'channelName':'/news/latest/company/us/xom', 'topic':'', '_':'1479888927416' } parameters2 = {'ticker':'XOM', 'countryCode':'US', 'dateTime':'12:38 p.m. Oct., 2016','docId':'', 'docType':'806' ,'sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2', 'messageNumber':'8525','count':'10', 'channelName':'/news/latest/company/us/xom', 'topic':'', '_':'1479888927417' }; firstUrl = "http://www.marketwatch.com/news/headline/getheadlines" html1 = requests.get(firstUrl, params = parameters1) result1 = (json.loads(html1.text)) html2 = requests.get(firstUrl, params = parameters2); result2 = (json.loads(html2.text)) 。您可以解码字符串,例如on this site

result1==result2

然后False将为.state('beers', { url: '/beers', templateUrl: 'app/beer/beer.html', controller: 'BeerCtrl', controllerAs: 'vm', resolve: { beers: function(BeerService) { return BeerService.query(function(data) { console.info(data); return data; }, function(error) { console.error(error); return error; }); } } })