1. 程式人生 > >Python爬蟲:帶參url的拼接

Python爬蟲:帶參url的拼接

 

如果連線直接這樣寫,看上去很直觀,不過引數替換不是很方便,而且看著不舒服

https://www.mysite.com/?sortField=%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD&pageIndex=3&pageSize=20

可以使用如下方式美化程式碼

from urllib.parse import urlencode 

url = "https://www.mysite.com/"

parameter = {
    "pageSize": 20,
    "sortField": "人工智慧",
    "pageIndex": 3 } data = urlencode(parameter) print(url + "?" + data) # https://www.mysite.com/?sortField=%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD&pageIndex=3&pageSize=20