
準備作業
查看肯德基官網的請求方法:post請求,

X-Requested-With: XMLHttpRequest 判斷得肯德基官網是ajax請求

通過這兩個準備步驟,明確本次爬蟲目標:
ajax的post請求肯德基官網 獲取上海肯德基地點前10頁,
分析
獲取上海肯德基地點前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.模擬瀏覽器向服務器發送請求 response回應
response = urllib.request.urlopen(url)
# 3.獲取回應中的頁面的原始碼 content內容
# read方法 回傳的是位元組形式的二進制資料
# 將二進制資料轉換為字串
# 二進制-->字串 解碼 decode方法
content = response.read().decode('utf-8')
# 4.列印資料
print(content)
- 定義一個url,就是你要訪問的地址
- 模擬瀏覽器向服務器發送請求 response回應
- 獲取回應中的頁面的原始碼 content內容
if __name__ == '__main__':
start_page = int(input('請輸入起始頁碼: '))
end_page = int(input('請輸入結束頁碼: '))
for page in range(start_page, end_page+1):
# 請求物件的定制
request = create_request(page)
# 獲取網頁原始碼
content = get_content(request)
# 下載資料
down_load(page, content)
對應的,我們在主函式中也類似宣告方法,
url組成資料定位

爬蟲的關鍵在于找介面,對于這個案例,在預覽頁可以找到頁面對應的json資料,說明這是我們要的資料,

構造url
不難發現,肯德基官網的url的一個共同點,我們把它保存為base_url,
base_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
引數
老樣子,找規律,只有’pageIndex’和頁碼有關,
data = {
'cname': '上海',
'pid': '',
'pageIndex': page,
'pageSize': '10'
}
post請求
- post請求的引數 必須要進行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
-
編碼之后必須呼叫encode方法
-
引數放在請求物件定制的方法中:post的請求的引數,是不會拼接在url后面的,而是放在請求物件定制的引數中
所以將data進行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
標頭獲取(防止反爬的一種手段)


即 回應頭中UA部分,
User Agent,用戶代理,特殊字串頭,使得服務器能夠識別客戶使用的作業系統及版本,CPU型別,瀏覽器及版本,瀏覽器內核,瀏覽器渲染引擎,瀏覽器語言,瀏覽器插件等,
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'
}
請求物件定制
引數,base_url,請求頭都準備得當后,就可以進行請求物件定制了,
request = urllib.request.Request(base_url,
headers=headers, data=data)
獲取網頁原始碼
把request請求作為引數,模擬瀏覽器向服務器發送請求 獲得response回應,
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
獲取回應中的頁面的原始碼,下載資料
使用 read()方法,得到位元組形式的二進制資料,需要使用 decode進行解碼,轉換為字串,
content = response.read().decode('utf-8')
然后我們將下載得到的資料寫進檔案,使用 with open() as fp 的語法,系統自動關閉檔案,
def down_load(page, content):
with open('kfc_' + str(page) + '.json', 'w', encoding='utf-8') as fp:
fp.write(content)
全部代碼
# ajax的post請求肯德基官網 獲取上海肯德基地點前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('請輸入結束頁碼: '))
for page in range(start_page, end_page+1):
# 請求物件的定制
request = create_request(page)
# 獲取網頁原始碼
content = get_content(request)
# 下載資料
down_load(page, content)
爬取后結果

鞠躬!!!其實還爬過Lisa的照片,想看爬蟲代碼的歡迎留言 !!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/309521.html
標籤:python
