您可以使用w3lib.url.add_or_replace_parameter
到query
參數附加到URL。它會以同樣的方式進行URL編碼爲蟒蛇,請求:
$ scrapy shell
2017-07-18 11:03:28 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: scrapybot)
(...)
>>> url = "https://services.realestate.com.au/services/listings/search"
>>> querystring = {"query":"{\"channel\":\"buy\",\"page\":2,\"pageSize\":12,\"filters\":{\"agencyIds\":[\"CBPHMG\"]}}"}
這是相同的輸入數據的python-requests
例子。
使用add_or_replace_parameter
與參數的名稱和它的值(注:Scrapy已經依賴於w3lib):
>>> from w3lib.url import add_or_replace_parameter
>>> add_or_replace_parameter(url, 'query', querystring['query'])
'https://services.realestate.com.au/services/listings/search?query=%7B%22channel%22%3A%22buy%22%2C%22page%22%3A2%2C%22pageSize%22%3A12%2C%22filters%22%3A%7B%22agencyIds%22%3A%5B%22CBPHMG%22%5D%7D%7D'
在這裏,Scrapy殼,獲取新的URL會得到一個JSON響應返回,如預期的那樣:
>>> new_url = add_or_replace_parameter(url, 'query', querystring['query'])
>>> fetch(new_url)
2017-07-18 11:04:45 [scrapy.core.engine] INFO: Spider opened
2017-07-18 11:04:46 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://services.realestate.com.au/services/listings/search?query=%7B%22channel%22%3A%22buy%22%2C%22page%22%3A2%2C%22pageSize%22%3A12%2C%22filters%22%3A%7B%22agencyIds%22%3A%5B%22CBPHMG%22%5D%7D%7D> (referer: None)
>>> import json
>>> data = json.loads(response.text)
>>> data.keys()
dict_keys(['prettyUrl', 'totalResultsCount', 'resolvedQuery', '_links', 'tieredResults', 'channel'])
>>> from pprint import pprint
>>> pprint(data)
{'_links': {'adCall': {'href': 'https://sasinator.realestate.com.au/rea/hserver/site=rea/area=buy.resultslist/proptype=villa/constructionStatus=established/sub=marsden/state=qld/pcode=4132/region=logan/price=200k_300k/platform={platform}/version={version}/pos={position}/size={size}/viewid={viewId}/random={random}',
'templated': True},
'canonical': {'href': 'http://www.realestate.com.au/buy/by-cbphmg/list-2'},
'exclusiveShowcaseUrl': {'href': 'https://services.realestate.com.au/services/listings/exclusiveShowcase?query=%7B%22propertyTypes%22:[],%22atlasIds%22:[],%22channel%22:%22buy%22%7D'},
'neighbourhoodsUrl': {'href': 'http://www.realestate.com.au/neighbourhoods?state=qld'},
'next': {},
'ofi': {'href': 'https://services.realestate.com.au/services/listings/ofi/{date}/daytotals?query=%7B%22channel%22:%22buy%22,%22pageSize%22:%2212%22,%22page%22:%222%22,%22filters%22:%7B%22agencyIds%22:%5B%22CBPHMG%22%5D%7D%7D',
'templated': True},
'prettyUrl': {'href': '/buy/by-cbphmg/list-2'},
'saveSearchUrl': {'href': 'https://www.realestate.com.au/saved-searches/#/save?search=%7B%22channel%22:%22buy%22,%22pageSize%22:%2212%22,%22page%22:%222%22,%22filters%22:%7B%22agencyIds%22:%5B%22CBPHMG%22%5D%7D%7D'},
'self': {'href': 'https://services.realestate.com.au/services/listings/search?query=%7B%22channel%22:%22buy%22,%22pageSize%22:%2212%22,%22page%22:%222%22,%22filters%22:%7B%22agencyIds%22:%5B%22CBPHMG%22%5D%7D%7D'}},
'channel': 'buy',
'prettyUrl': '/buy/by-cbphmg/list-2',
'resolvedQuery': {'channel': 'buy',
'filters': {'agencyIds': ['CBPHMG']},
'page': '2',
'pageSize': '12'},
'tieredResults': [{'count': 11,
'results': [{...}],
'tier': 1}],
'totalResultsCount': 23}