http://www.kfc.com.cn 爬取(案例練習:ajax、post)
阿新 • • 發佈:2018-12-18
#!/usr/bin/env python # -*- coding: utf-8 -*- import urllib import urllib.request import urllib.parse # ajax post post_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=pid' headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36' } if __name__ == '__main__': city = input("請輸入您要查詢的城市:") page = int(input("請輸入查詢多少頁:")) for p in range(1, page+1): form_data = { 'cname': city, 'pid': 31, 'pageIndex': p, 'pageSize': 50 } # 因為瀏覽器會自動將我們傳遞的表哥引數進行編碼,所以我們也要模擬瀏覽器將表格引數進行編碼 form_data = urllib.parse.urlencode(form_data).encode('utf-8') headers_request = urllib.request.Request(url=post_url, data=form_data,headers=headers) response = urllib.request.urlopen(headers_request) content = response.read().decode('utf-8') with open('./肯德基第%d頁.json' % (p,), mode='wb') as fp: fp.write(content.encode('utf-8'))