- 基于如下5點(diǎn)展開requests模塊的學(xué)習(xí)
-
什么是requests模塊
- requests模塊是python中原生的基于網(wǎng)絡(luò)請求的模塊,其主要作用是用來模擬瀏覽器發(fā)起請求。功能強(qiáng)大,用法簡潔高效。在爬蟲領(lǐng)域中占據(jù)著半壁江山的地位。
-
為什么要使用requests模塊
-
因?yàn)樵谑褂胾rllib模塊的時(shí)候,會(huì)有諸多不便之處,總結(jié)如下:
- 手動(dòng)處理url編碼
- 手動(dòng)處理post請求參數(shù)
- 處理cookie和代理操作繁瑣
- ......
-
使用requests模塊:
- 自動(dòng)處理url編碼
- 自動(dòng)處理post請求參數(shù)
- 簡化cookie和代理操作
- ......
-
因?yàn)樵谑褂胾rllib模塊的時(shí)候,會(huì)有諸多不便之處,總結(jié)如下:
-
如何使用requests模塊
-
安裝:
- pip install requests
-
使用流程
- 指定url
- 基于requests模塊發(fā)起請求
- 獲取響應(yīng)對象中的數(shù)據(jù)值
- 持久化存儲(chǔ)
-
安裝:
-
通過5個(gè)基于requests模塊的爬蟲項(xiàng)目對該模塊進(jìn)行學(xué)習(xí)和鞏固
-
基于requests模塊的get請求
- 需求:爬取搜狗指定詞條搜索后的頁面數(shù)據(jù)
-
基于requests模塊的post請求
- 需求:登錄豆瓣電影,爬取登錄成功后的頁面數(shù)據(jù)
-
基于requests模塊ajax的get請求
- 需求:爬取豆瓣電影分類排行榜?https://movie.douban.com/中的電影詳情數(shù)據(jù)
-
基于requests模塊ajax的post請求
- 需求:爬取肯德基餐廳查詢http://www.kfc.com.cn/kfccda/index.aspx中指定地點(diǎn)的餐廳數(shù)據(jù)
-
綜合練習(xí)
- 需求:爬取國家藥品監(jiān)督管理總局中基于中華人民共和國化妝品生產(chǎn)許可證相關(guān)數(shù)據(jù)http://125.35.6.84:81/xk/
-
基于requests模塊的get請求
- 代碼展示
-
需求:爬取搜狗指定詞條搜索后的頁面數(shù)據(jù)
import requests import os #指定搜索關(guān)鍵字 word = input( 'enter a word you want to search:') #自定義請求頭信息 headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', } #指定url url = 'https://www.sogou.com/web' #封裝get請求參數(shù) prams = { 'query':word, 'ie': 'utf-8' } #發(fā)起請求 response = requests.get(url=url,params=param) #獲取響應(yīng)數(shù)據(jù) page_text = response.text with open( './sougou.html', 'w',encoding= 'utf-8') as fp: fp.write(page_text)
請求載體身份標(biāo)識(shí)的偽裝:
-
User-Agent:請求載體身份標(biāo)識(shí),通過瀏覽器發(fā)起的請求,請求載體為瀏覽器,則該請求的User-Agent為瀏覽器的身份標(biāo)識(shí),使用爬蟲程序發(fā)起的請求,則該請求的載體為爬蟲程序,則該請求的User-Agent為爬蟲程序的身份標(biāo)識(shí)。可以通過判斷該值來獲知該請求的載體究竟是基于哪款瀏覽器還是基于爬蟲程序。
-
反爬機(jī)制:某些門戶網(wǎng)站會(huì)對訪問該網(wǎng)站的請求中的User-Agent進(jìn)行捕獲和判斷,如果該請求的UA為爬蟲程序,則拒絕向該請求提供數(shù)據(jù)。
-
反反爬策略:將爬蟲程序的UA偽裝成某一款瀏覽器的身份標(biāo)識(shí)。
-
-
需求:登錄豆瓣電影,爬取登錄成功后的頁面數(shù)據(jù)
import requests import os url = 'https://accounts.douban.com/login' #封裝請求參數(shù) data = { "source": "movie", "redir": "https://movie.douban.com/", "form_email": "15027900535", "form_password": "bobo@15027900535", "login": "登錄", } #自定義請求頭信息 headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', } response = requests.post(url=url,data=data) page_text = response.text with open( './douban111.html', 'w',encoding= 'utf-8') as fp: fp.write(page_text)
?
需求:爬取豆瓣電影分類排行榜?https://movie.douban.com/中的電影詳情數(shù)據(jù)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import urllib.request
if __name__ ==
"__main__":
#指定ajax-get請求的url(通過抓包進(jìn)行獲取) url =
'https://movie.douban.com/j/chart/top_list?'
#定制請求頭信息,相關(guān)的頭信息必須封裝在字典結(jié)構(gòu)中 headers = {
#定制請求頭中的User-Agent參數(shù),當(dāng)然也可以定制請求頭中其他的參數(shù)
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36', }
#定制get請求攜帶的參數(shù)(從抓包工具中獲取) param = {
'type':
'5',
'interval_id':
'100:90',
'action':
'',
'start':
'0',
'limit':
'20' }
#發(fā)起get請求,獲取響應(yīng)對象 response = requests.get(url=url,headers=headers,params=param)
#獲取響應(yīng)內(nèi)容:響應(yīng)內(nèi)容為json串 print(response.text)
需求:爬取肯德基餐廳查詢http://www.kfc.com.cn/kfccda/index.aspx中指定地點(diǎn)的餐廳數(shù)據(jù)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import urllib.request
if __name__ ==
"__main__":
#指定ajax-post請求的url(通過抓包進(jìn)行獲取) url =
'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
#定制請求頭信息,相關(guān)的頭信息必須封裝在字典結(jié)構(gòu)中 headers = {
#定制請求頭中的User-Agent參數(shù),當(dāng)然也可以定制請求頭中其他的參數(shù)
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36', }
#定制post請求攜帶的參數(shù)(從抓包工具中獲取) data = {
'cname':
'',
'pid':
'',
'keyword':
'北京',
'pageIndex':
'1',
'pageSize':
'10' }
#發(fā)起post請求,獲取響應(yīng)對象 response = requests.get(url=url,headers=headers,data=data)
#獲取響應(yīng)內(nèi)容:響應(yīng)內(nèi)容為json串 print(response.text)
-
需求:爬取國家藥品監(jiān)督管理總局中基于中華人民共和國化妝品生產(chǎn)許可證相關(guān)數(shù)據(jù)
import requests from fake_useragent import UserAgent ua = UserAgent(use_cache_server= False,verify_ssl= False).random headers = { 'User-Agent':ua } url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList' pageNum = 3 for page in range( 3, 5): data = { 'on': 'true', 'page': str(page), 'pageSize': '15', 'productName': '', 'conditionType': '1', 'applyname': '', 'applysn': '' } json_text = requests.post(url=url,data=data,headers=headers).json() all_id_list = [] for dict in json_text[ 'list']: id = dict[ 'ID'] #用于二級頁面數(shù)據(jù)獲取 #下列詳情信息可以在二級頁面中獲取 # name = dict['EPS_NAME'] # product = dict['PRODUCT_SN'] # man_name = dict['QF_MANAGER_NAME'] # d1 = dict['XC_DATE'] # d2 = dict['XK_DATE'] all_id_list.append(id) #該url是一個(gè)ajax的post請求 post_url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById' for id in all_id_list: post_data = { 'id':id } response = requests.post(url=post_url,data=post_data,headers=headers) #該請求響應(yīng)回來的數(shù)據(jù)有兩個(gè),一個(gè)是基于text,一個(gè)是基于json的,所以可以根據(jù)content-type,來獲取指定的響應(yīng)數(shù)據(jù) if response.headers[ 'Content-Type'] == 'application/json;charset=UTF-8': #print(response.json()) #進(jìn)行json解析 json_text = response.json() print(json_text[ 'businessPerson'])
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
