準(zhǔn)備工作
查看肯德基官網(wǎng)的請(qǐng)求方法:post請(qǐng)求。
X-Requested-With: XMLHttpRequest 判斷得肯德基官網(wǎng)是ajax請(qǐng)求
通過(guò)這兩個(gè)準(zhǔn)備步驟,明確本次爬蟲目標(biāo):
ajax的post請(qǐng)求肯德基官網(wǎng) 獲取上海肯德基地點(diǎn)前10頁(yè)。
分析
獲取上海肯德基地點(diǎn)前10頁(yè),那就需要先對(duì)每頁(yè)的url進(jìn)行分析。
第一頁(yè)
# page1 # http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname # POST # cname: 上海 # pid: # pageIndex: 1 # pageSize: 10
第二頁(yè)
# page2 # http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname # POST # cname: 上海 # pid: # pageIndex: 2 # pageSize: 10
第三頁(yè)依次類推。
程序入口
首先回顧urllib爬取的基本操作:
# 使用urllib獲取百度首頁(yè)的源碼 import urllib.request # 1.定義一個(gè)url,就是你要訪問(wèn)的地址 url = 'http://www.baidu.com' # 2.模擬瀏覽器向服務(wù)器發(fā)送請(qǐng)求 response響應(yīng) response = urllib.request.urlopen(url) # 3.獲取響應(yīng)中的頁(yè)面的源碼 content內(nèi)容 # read方法 返回的是字節(jié)形式的二進(jìn)制數(shù)據(jù) # 將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為字符串 # 二進(jìn)制-->字符串 解碼 decode方法 content = response.read().decode('utf-8') # 4.打印數(shù)據(jù) print(content)
1.定義一個(gè)url,就是你要訪問(wèn)的地址
2.模擬瀏覽器向服務(wù)器發(fā)送請(qǐng)求 response響應(yīng)
3.獲取響應(yīng)中的頁(yè)面的源碼 content內(nèi)容
if __name__ == '__main__': start_page = int(input('請(qǐng)輸入起始頁(yè)碼: ')) end_page = int(input('請(qǐng)輸入結(jié)束頁(yè)碼: ')) for page in range(start_page, end_page+1): # 請(qǐng)求對(duì)象的定制 request = create_request(page) # 獲取網(wǎng)頁(yè)源碼 content = get_content(request) # 下載數(shù)據(jù) down_load(page, content)
對(duì)應(yīng)的,我們?cè)谥骱瘮?shù)中也類似聲明方法。
url組成數(shù)據(jù)定位
爬蟲的關(guān)鍵在于找接口。對(duì)于這個(gè)案例,在預(yù)覽頁(yè)可以找到頁(yè)面對(duì)應(yīng)的json數(shù)據(jù),說(shuō)明這是我們要的數(shù)據(jù)。
構(gòu)造url
不難發(fā)現(xiàn),肯德基官網(wǎng)的url的一個(gè)共同點(diǎn),我們把它保存為base_url。
base_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
參數(shù)
老樣子,找規(guī)律,只有'pageIndex'和頁(yè)碼有關(guān)。
data = { 'cname': '上海', 'pid': '', 'pageIndex': page, 'pageSize': '10' }
post請(qǐng)求
- post請(qǐng)求的參數(shù) 必須要進(jìn)行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
- 編碼之后必須調(diào)用encode方法
- 參數(shù)放在請(qǐng)求對(duì)象定制的方法中:post的請(qǐng)求的參數(shù),是不會(huì)拼接在url后面的,而是放在請(qǐng)求對(duì)象定制的參數(shù)中
所以將data進(jìn)行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
標(biāo)頭獲取(防止反爬的一種手段)
即 響應(yīng)頭中UA部分。
User Agent,用戶代理,特殊字符串頭,使得服務(wù)器能夠識(shí)別客戶使用的操作系統(tǒng)及版本,CPU類型,瀏覽器及版本,瀏覽器內(nèi)核,瀏覽器渲染引擎,瀏覽器語(yǔ)言,瀏覽器插件等。
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38' }
請(qǐng)求對(duì)象定制
參數(shù),base_url,請(qǐng)求頭都準(zhǔn)備得當(dāng)后,就可以進(jìn)行請(qǐng)求對(duì)象定制了。
request = urllib.request.Request(base_url, headers=headers, data=data)
獲取網(wǎng)頁(yè)源碼
把request請(qǐng)求作為參數(shù),模擬瀏覽器向服務(wù)器發(fā)送請(qǐng)求 獲得response響應(yīng)。
response = urllib.request.urlopen(request) content = response.read().decode('utf-8')
獲取響應(yīng)中的頁(yè)面的源碼,下載數(shù)據(jù)
使用 read()方法,得到字節(jié)形式的二進(jìn)制數(shù)據(jù),需要使用 decode進(jìn)行解碼,轉(zhuǎn)換為字符串。
content = response.read().decode('utf-8')
然后我們將下載得到的數(shù)據(jù)寫進(jìn)文件,使用 with open() as fp 的語(yǔ)法,系統(tǒng)自動(dòng)關(guān)閉文件。
def down_load(page, content): with open('kfc_' + str(page) + '.json', 'w', encoding='utf-8') as fp: fp.write(content)
全部代碼
# ajax的post請(qǐng)求肯德基官網(wǎng) 獲取上海肯德基地點(diǎn)前10頁(yè) # page1 # http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname # POST # cname: 上海 # pid: # pageIndex: 1 # pageSize: 10 # page2 # http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname # POST # cname: 上海 # pid: # pageIndex: 2 # pageSize: 10 import urllib.request, urllib.parse def create_request(page): base_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname' data = { 'cname': '上海', 'pid': '', 'pageIndex': page, 'pageSize': '10' } data = urllib.parse.urlencode(data).encode('utf-8') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38' } request = urllib.request.Request(base_url, headers=headers, data=data) return request def get_content(request): response = urllib.request.urlopen(request) content = response.read().decode('utf-8') return content def down_load(page, content): with open('kfc_' + str(page) + '.json', 'w', encoding='utf-8') as fp: fp.write(content) if __name__ == '__main__': start_page = int(input('請(qǐng)輸入起始頁(yè)碼: ')) end_page = int(input('請(qǐng)輸入結(jié)束頁(yè)碼: ')) for page in range(start_page, end_page+1): # 請(qǐng)求對(duì)象的定制 request = create_request(page) # 獲取網(wǎng)頁(yè)源碼 content = get_content(request) # 下載數(shù)據(jù) down_load(page, content)
爬取后結(jié)果
以上就是Python爬取肯德基官網(wǎng)ajax的post請(qǐng)求實(shí)現(xiàn)過(guò)程的詳細(xì)內(nèi)容,更多關(guān)于Python爬取肯德基官網(wǎng)ajax的post請(qǐng)求的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/Joy_Cheung666/article/details/120710070