爬蟲基本概念
爬蟲的定義
- 網絡爬蟲(被稱為 網頁蜘蛛,網絡機器人 ),就是 模擬客戶端發(fā)送網絡請求 ,接收請求響應,一種按照一定的規(guī)則,自動地抓取互聯(lián)網信息的程序
- 一般來說,只要瀏覽器上能做的事情,爬蟲都可以做
**爬蟲的分類
**
- 通用爬蟲 :通常指搜索引擎的爬蟲
- 聚集爬蟲 :針對特定網站或某些特定網頁的爬蟲
ROBOTS協(xié)議
- Robots協(xié)議: 也叫robots.txt(統(tǒng)一小寫)是一種存放于網站根目錄下的ASCII編碼的文本文件,它通常告訴網絡搜索引擎的漫游器(又稱網絡蜘蛛),此網站中的哪些內容是不應被搜索引擎的漫游器獲取的,哪些是可以被漫游器獲取的
- 如果將網站視為酒店里的一個房間,robots.txt就是主人在房間門口懸掛的“請勿打擾”或“歡迎打掃”的提示牌。這個文件告訴來訪的搜索引擎哪些房間可以進入和參觀,哪些房間因為存放貴重物品,或可能涉及住戶及訪客的隱私而不對搜索引擎開放。但robots.txt不是命令,也不是防火墻,如同守門人無法阻止竊賊等惡意闖入者
舉例:
-
訪問淘寶網的robots文件: https://www.taobao.com/robots.txt
* 很顯然淘寶不允許百度的機器人訪問其網站下其所有的目錄
-
如果允許所有的鏈接訪問應該是:Allow:/
http和https的概念
-
http
- 超文本傳輸協(xié)議
- 默認端口號:80
-
https
- HTTP+SSL(安全套接字層)
- 默認端口號:443
-
**HTTPS比HTTP更安全,但是性能更低
**
URL的形式
- sheme://host[:port#]/path/.../[?query-string][#anchor]
Http常見請求頭
- Host (主機和端口號)
- Connection(鏈接類型)
- Upgrade-insecure-Requests(升級為https請求)
- User-Agent(瀏覽器名稱)
- Accept(傳輸文件類型)
- Referer(頁面跳轉處)
- Accept-Encoding(文件編解碼格式)
- Cookie
- x-requested-with:XMLHttpRequest(是Ajax異步請求)
Http常見響應碼
- 200:成功
- 302:臨時性重定向到新的url
- 404:請求的路徑找不到
- 500:服務器內部錯誤
rquests模塊
requests的官網
**示例:下載官網上的圖片
**
import requests
url="https://docs.python-requests.org/zh_CN/latest/_static/requests-sidebar.png"
response=requests.get(url)
# 響應狀態(tài)碼為200,表示請求成功
if response.status_code==200:
# 生成圖片
with open("aa.png","wb") as f:
f.write(response.content)
response.text 和 response.content 的區(qū)別
- response.text
- 類型:str
- 解碼類型:根據(jù)http頭部對響應的編碼作出有根據(jù)的推測,推測的文本編碼
- 修改編碼的方法:response.encoding='gbk'
- response.content
- 類型:bytes
- 解碼類型:沒有指定
- 修改編碼的方法:response.content.decode('utf-8')
**requests請求帶header
**
- 主要是模擬瀏覽器,欺騙服務器,獲取和瀏覽器一致的內容
- header的數(shù)據(jù)結構是 字典
示例
import requests
respons=requests.get("http://www.baidu.com")
print(respons.request.headers)
# 設置header 模擬谷歌瀏覽器
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}
respons2=requests.get("http://www.baidu.com",headers=headers)
print(respons2.request.headers)
獲取User-Agent的值
帶參數(shù)的requests請求
import requests
url="http://www.baidu.com/s?"
# 添加參數(shù)
params={
"wd":"hello"
}
# 方式一
respons=requests.get(url,params=params)
# 方式二
respons2=requests.get("http://www.baidu.com/s?wd={}".format("hello"))
print(respons.status_code)
print(respons.request.url)
print(respons2.status_code)
print(respons2.request.url)
requests發(fā)送post請求
- 語法
- response=requests.post("http://www.baidu.com/",data=data,headers=headers)
- data的數(shù)據(jù)結構是 字典
- 示例
import requests
url="https://www.jisilu.cn/data/cbnew/cb_list/?___jsl=LST___t=1617719326771"
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}
params={
"curr_iss_amt": 20
}
respons=requests.post(url,data=params,headers=headers)
print(respons.content.decode())
requests使用代理方式
- 使用代理的原因
- 讓服務器以為不是同一個客戶端請求
- 防止我們的真實地址被泄露,防止被追究
- 語法
- requetst.get("http://www.baidu.com",proxies=proxies)
- proxies的數(shù)據(jù)結構 字典
- proxies={ "http":"http://12.33.34.54:8374","https":"https://12.33.34.54:8374" }
- 示例
import requests
url="http://www.baidu.com"
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}
proxies={
"http": "175.42.158.211:9999"
}
respons=requests.post(url,proxies=proxies)
print(respons.status_code)
**cookie和session的區(qū)別
**
- cookie數(shù)據(jù)存放在客戶的瀏覽器上,session數(shù)據(jù)放在服務器上
- cookie不是很安全,別人可以分析存放在本地的cookie并進行cookie欺騙
- session會在一定時間內保存在服務器上。當訪問增多會比較占用服務器性能
- 單個cookie保存的數(shù)據(jù)不能超過4k,很多瀏覽器都限制一個站點最多保存20個cookie
爬蟲處理cookie和session
- 帶上cookie、session的好處
- 能夠請求到登錄之后的頁面
- 帶上cookie、session的壞處
- 一套cookie和session往往和一個用戶對應,請求太快,請求次數(shù)太多,容易被服務器識別為爬蟲
- 如果不需要cookie的時候盡量不去使用cookie
**requests 處理cookies、session請求
**
- reqeusts 提供了一個session類,來實現(xiàn)客戶端和服務端的會話保持
- 使用方法
- 實例化一個session對象
- 讓session發(fā)送get或者post請求
- 語法
- session=requests.session()
- response=session.get(url,headers)
- 示例
import requests
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}
data={
"email":"用戶名",
"password":"密碼"
}
session=requests.session()
# session發(fā)送post請求,cookie保存在其中
session.post("http://www.renren.com/PLogin.do",data=data,headers=headers)
# 使用session請求登錄后才能訪問的頁面
r=session.get("http://www.renren.com/976564425",headers=headers)
print(r.content.decode())
-
互聯(lián)網
+關注
關注
54文章
11187瀏覽量
103871 -
程序
+關注
關注
117文章
3796瀏覽量
81416 -
網絡爬蟲
+關注
關注
1文章
52瀏覽量
8722
發(fā)布評論請先 登錄
相關推薦
評論