Python實戰(zhàn)練習(xí)之終于對肯德基下手
準(zhǔn)備工作
查看肯德基官網(wǎng)的請求方法:post請求。
X-Requested-With: XMLHttpRequest
判斷得肯德基官網(wǎng)是ajax
請求
通過這兩個準(zhǔn)備步驟,明確本次爬蟲目標(biāo):
ajax的post請求肯德基官網(wǎng) 獲取上??系禄攸c前10頁。
分析
獲取上??系禄攸c前10頁,那就需要先對每頁的url進(jìn)行分析。
第一頁
# 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.模擬瀏覽器向服務(wù)器發(fā)送請求 response響應(yīng) response = urllib.request.urlopen(url) # 3.獲取響應(yīng)中的頁面的源碼 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)
- 定義一個url,就是你要訪問的地址
- 模擬瀏覽器向服務(wù)器發(fā)送請求 response響應(yīng)
- 獲取響應(yīng)中的頁面的源碼 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)
對應(yīng)的,我們在主函數(shù)中也類似聲明方法。
url組成數(shù)據(jù)定位
爬蟲的關(guān)鍵在于找接口。對于這個案例,在預(yù)覽頁可以找到頁面對應(yīng)的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ù) 必須要進(jìn)行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
- 編碼之后必須調(diào)用encode方法
- 參數(shù)放在請求對象定制的方法中:post的請求的參數(shù),是不會拼接在url后面的,而是放在請求對象定制的參數(shù)中
所以將data進(jìn)行編碼
data = urllib.parse.urlencode(data).encode('utf-8')
標(biāo)頭獲?。ǚ乐狗磁赖囊环N手段)
即 響應(yīng)頭中UA部分。
User Agent,用戶代理,特殊字符串頭,使得服務(wù)器能夠識別客戶使用的操作系統(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,請求頭都準(zhǔn)備得當(dāng)后,就可以進(jìn)行請求對象定制了。
request = urllib.request.Request(base_url, headers=headers, data=data)
獲取網(wǎng)頁源碼
把request請求作為參數(shù),模擬瀏覽器向服務(wù)器發(fā)送請求 獲得response響應(yīng)。
response = urllib.request.urlopen(request) content = response.read().decode('utf-8')
獲取響應(yīng)中的頁面的源碼,下載數(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
的語法,系統(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é)果
鞠躬?。?!其實還爬過Lisa的照片,想看爬蟲代碼的歡迎留言 ?。?!
到此這篇關(guān)于Python實戰(zhàn)練習(xí)之終于對肯德基下手的文章就介紹到這了,更多相關(guān)Python 肯德基官網(wǎng)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。