Python爬取肯德基官網(wǎng)ajax的post請求實現(xiàn)過程

準備工作
查看肯德基官網(wǎng)的請求方法:post請求。

X-Requested-With: XMLHttpRequest 判斷得肯德基官網(wǎng)是ajax請求

通過這兩個準備步驟,明確本次爬蟲目標:
ajax的post請求肯德基官網(wǎng) 獲取上海肯德基地點前10頁。
分析
獲取上??系禄攸c前10頁,那就需要先對每頁的url進行分析。
第一頁
# 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
第三頁依次類推。
程序入口
首先回顧urllib爬取的基本操作:
# 使用urllib獲取百度首頁的源碼
import urllib.request
# 1.定義一個url,就是你要訪問的地址
url = 'http://www.baidu.com'
# 2.模擬瀏覽器向服務器發(fā)送請求 response響應
response = urllib.request.urlopen(url)
# 3.獲取響應中的頁面的源碼 content內(nèi)容
# read方法 返回的是字節(jié)形式的二進制數(shù)據(jù)
# 將二進制數(shù)據(jù)轉(zhuǎn)換為字符串
# 二進制-->字符串 解碼 decode方法
content = response.read().decode('utf-8')
# 4.打印數(shù)據(jù)
print(content)
1.定義一個url,就是你要訪問的地址
2.模擬瀏覽器向服務器發(fā)送請求 response響應
3.獲取響應中的頁面的源碼 content內(nèi)容
if __name__ == '__main__':
start_page = int(input('請輸入起始頁碼: '))
end_page = int(input('請輸入結(jié)束頁碼: '))
for page in range(start_page, end_page+1):
# 請求對象的定制
request = create_request(page)
# 獲取網(wǎng)頁源碼
content = get_content(request)
# 下載數(shù)據(jù)
down_load(page, content)
對應的,我們在主函數(shù)中也類似聲明方法。
url組成數(shù)據(jù)定位

爬蟲的關(guān)鍵在于找接口。對于這個案例,在預覽頁可以找到頁面對應的json數(shù)據(jù),說明這是我們要的數(shù)據(jù)。

構(gòu)造url
不難發(fā)現(xiàn),肯德基官網(wǎng)的url的一個共同點,我們把它保存為base_url。
base_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
參數(shù)
老樣子,找規(guī)律,只有'pageIndex'和頁碼有關(guān)。
data = {
'cname': '上海',
'pid': '',
'pageIndex': page,
'pageSize': '10'
}
post請求
- post請求的參數(shù) 必須要進行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
- 編碼之后必須調(diào)用encode方法
- 參數(shù)放在請求對象定制的方法中:post的請求的參數(shù),是不會拼接在url后面的,而是放在請求對象定制的參數(shù)中
所以將data進行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
標頭獲取(防止反爬的一種手段)


即 響應頭中UA部分。
User Agent,用戶代理,特殊字符串頭,使得服務器能夠識別客戶使用的操作系統(tǒng)及版本,CPU類型,瀏覽器及版本,瀏覽器內(nèi)核,瀏覽器渲染引擎,瀏覽器語言,瀏覽器插件等。
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'
}
請求對象定制
參數(shù),base_url,請求頭都準備得當后,就可以進行請求對象定制了。
request = urllib.request.Request(base_url, headers=headers, data=data)
獲取網(wǎng)頁源碼
把request請求作為參數(shù),模擬瀏覽器向服務器發(fā)送請求 獲得response響應。
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
獲取響應中的頁面的源碼,下載數(shù)據(jù)
使用 read()方法,得到字節(jié)形式的二進制數(shù)據(jù),需要使用 decode進行解碼,轉(zhuǎn)換為字符串。
content = response.read().decode('utf-8')
然后我們將下載得到的數(shù)據(jù)寫進文件,使用 with open() as fp 的語法,系統(tǒ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請求肯德基官網(wǎng) 獲取上??系禄攸c前10頁
# 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('請輸入起始頁碼: '))
end_page = int(input('請輸入結(jié)束頁碼: '))
for page in range(start_page, end_page+1):
# 請求對象的定制
request = create_request(page)
# 獲取網(wǎng)頁源碼
content = get_content(request)
# 下載數(shù)據(jù)
down_load(page, content)
爬取后結(jié)果

以上就是Python爬取肯德基官網(wǎng)ajax的post請求實現(xiàn)過程的詳細內(nèi)容,更多關(guān)于Python爬取肯德基官網(wǎng)ajax的post請求的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python+matplotlib調(diào)用隨機函數(shù)生成變化圖形
這篇文章主要介紹了如何在Python中利用隨機函數(shù)生成變化的圖形,文中的示例代碼講解詳細,對我們學習有一定吧參考價值,需要的可以了解一下2022-04-04
Python實現(xiàn)獲取操作系統(tǒng)版本信息方法
這篇文章主要介紹了Python實現(xiàn)獲取操作系統(tǒng)版本信息方法,本文在命令行中獲取操作系統(tǒng)信息,介紹了platform模塊的使用,需要的朋友可以參考下2015-04-04
關(guān)于DataFrame中某列值的替換map(dict)
這篇文章主要介紹了關(guān)于DataFrame中某列值的替換map(dict),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
python中import和from-import的區(qū)別解析
這篇文章主要介紹了python中import和from-import的區(qū)別解析,本文通過實例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Django JSonResponse對象的實現(xiàn)
本文主要介紹了Django JSonResponse對象的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03

