我在python中编写了一个脚本来从网页中抓取一些信息。该网站需要get
请求方法。我现在面临的问题是,parameters
需要与url
合并,因此它应该是urlencoded
。这就是我被困住的地方。我无法正确编码以获得有效的响应。我试了一下,但它没带任何
我正在尝试的脚本:
import requests
import urllib.parse
fields ={
'/API/api/v1/Search/Properties/?f':'319 lizzie','ty':'2018','pvty':'2017','pn':'1','st':'9','so':'1','pt':'RP;PP;MH;NR','take':'20','skip':'0','page':'1','pageSize':'20'
}
payload = urllib.parse.quote_plus(fields, safe='', encoding=None, errors=None)
headers={
"User-Agent":"Mozilla/5.0"
}
page = requests.get("http://search.wcad.org/Proxy/APIProxy.ashx?", params=payload, headers=headers)
print(page.json())
上述网址应如下所示:
http://search.wcad.org/Proxy/APIProxy.ashx?/API/api/v1/Search/Properties/?f=319%20LIZZIE&ty=2018&pvty=2017&pn=1&st=9&so=1&pt=RP%3BPP%3BMH%3BNR&take=20&skip=0&page=1&pageSize=20
获得回复。
顺便说一句,这是我对现有脚本的错误:
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python35-32\Social.py", line 9, in <module>
payload = urllib.parse.quote_plus(fields, safe='', encoding=None, errors=None)
File "C:\Users\ar\AppData\Local\Programs\Python\Python35-32\lib\urllib\parse.py", line 728, in quote_plus
string = quote(string, safe + space, encoding, errors)
File "C:\Users\ar\AppData\Local\Programs\Python\Python35-32\lib\urllib\parse.py", line 712, in quote
return quote_from_bytes(string, safe)
File "C:\Users\ar\AppData\Local\Programs\Python\Python35-32\lib\urllib\parse.py", line 737, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
答案 0 :(得分:1)
这很有效。如the documentation所示,您无需自行进行任何URL编码。
关键是查询字符串从最后一个问号开始,而不是从第一个开始。在网址中包含第二个问号是必需的,因为/login:username,password
只会在没有一个问号时添加一个。
requests
结果
{ 'ResultList': [{ 'PropertyQuickRefID': 'R016698', 'PartyQuickRefID': 'O0485204', 'OwnerQuickRefID': 'R016698', 'LegacyID': None, 'PropertyNumber': 'R-13-0410-0620-50000', 'OwnerName': 'GOOCH, PHILIP L', 'SitusAddress': '319 LIZZIE ST, TAYLOR, TX 76574', 'PropertyValue': 46785.0, 'LegalDescription': 'DOAK ADDITION, BLOCK 62, LOT 5', 'NeighborhoodCode': 'T541', 'Abstract': None, 'Subdivision': 'S3564 - Doak Addition', 'PropertyType': 'Real', 'ID': 0, 'Text': None, 'TaxYear': 2018, 'PropertyValueTaxYear': 2017 }], 'HasMoreData': False, 'TotalPageCount': 1, 'CurrentPage': 1, 'RecordCount': 1, 'SearchText': '319 lizzie', 'PagingHandledByCaller': False, 'TaxYear': 2018, 'PropertyValueTaxYear': 0 }