10個python爬蟲入門實例


昨天帶伙伴萌學習python爬蟲,准備了幾個簡單的入門實例

涉及主要知識點:

  1. web是如何交互的
  2. requests庫的get、post函數的應用
  3. response對象的相關函數,屬性
  4. python文件的打開,保存

代碼中給出了注釋,並且可以直接運行哦

如何安裝requests庫(安裝好python的朋友可以直接參考,沒有的,建議先裝一哈python環境)

windows用戶,Linux用戶幾乎一樣:

打開cmd輸入以下命令即可,如果python的環境在C盤的目錄,會提示權限不夠,只需以管理員方式運行cmd窗口

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

點擊並拖拽以移動

Linux用戶類似(ubantu為例): 權限不夠的話在命令前加入sudo即可

sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

點擊並拖拽以移動

1.爬取強大的BD頁面,打印頁面信息

# 第一個爬蟲示例,爬取百度頁面

import requests #導入爬蟲的庫,不然調用不了爬蟲的函數

response = requests.get("http://www.baidu.com")  #生成一個response對象

response.encoding = response.apparent_encoding #設置編碼格式

print("狀態碼:"+ str( response.status_code ) ) #打印狀態碼

print(response.text)#輸出爬取的信息

點擊並拖拽以移動

2.常用方法之get方法實例,下面還有傳參實例

# 第二個get方法實例

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

response = requests.get("http://httpbin.org/get")  #get方法

print( response.status_code ) #狀態碼

print( response.text )

點擊並拖拽以移動

3. 常用方法之post方法實例,下面還有傳參實例

# 第三個 post方法實例

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

response = requests.post("http://httpbin.org/post")  #post方法訪問

print( response.status_code ) #狀態碼

print( response.text )

點擊並拖拽以移動

4. put方法實例

# 第四個 put方法實例

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

response = requests.put("http://httpbin.org/put")  # put方法訪問

print( response.status_code ) #狀態碼

print( response.text )

點擊並拖拽以移動

5.常用方法之get方法傳參實例(1)

如果需要傳多個參數只需要用&符號連接即可如下

# 第五個 get傳參方法實例

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

response = requests.get("http://httpbin.org/get?name=hezhi&age=20")  # get傳參

print( response.status_code ) #狀態碼

print( response.text )

點擊並拖拽以移動

6.常用方法之get方法傳參實例(2)

params用字典可以傳多個

# 第六個 get傳參方法實例

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

data = {
	"name":"hezhi",
	"age":20
}
response = requests.get( "http://httpbin.org/get" , params=data )  # get傳參

print( response.status_code ) #狀態碼

print( response.text )

點擊並拖拽以移動

7.常用方法之post方法傳參實例(2) 和上一個有沒有很像

# 第七個 post傳參方法實例

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

data = {
	"name":"hezhi",
	"age":20
}
response = requests.post( "http://httpbin.org/post" , params=data )  # post傳參

print( response.status_code ) #狀態碼

print( response.text )

點擊並拖拽以移動

8.關於繞過反爬機制,以zh爸爸為例

# 第好幾個方法實例

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

response = requests.get( "http://www.zhihu.com")  #第一次訪問知乎,不設置頭部信息

print( "第一次,不設頭部信息,狀態碼:"+response.status_code )# 沒寫headers,不能正常爬取,狀態碼不是 200

#下面是可以正常爬取的區別,更改了User-Agent字段

headers = {

		"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"

}#設置頭部信息,偽裝瀏覽器

response = requests.get( "http://www.zhihu.com" , headers=headers )  #get方法訪問,傳入headers參數,

print( response.status_code ) # 200!訪問成功的狀態碼

print( response.text )

點擊並拖拽以移動

9.爬取信息並保存到本地,

因為目錄關系,在D盤建立了一個叫做爬蟲的文件夾,然后保存信息

注意文件保存時的encoding設置

# 爬取一個html並保存

import requests

url = "http://www.baidu.com"

response = requests.get( url )

response.encoding = "utf-8" #設置接收編碼格式

print("\nr的類型" + str( type(response) ) )

print("\n狀態碼是:" + str( response.status_code ) )

print("\n頭部信息:" + str( response.headers ) )

print( "\n響應內容:" )

print( response.text )

#保存文件
file = open("D:\\爬蟲\\baidu.html","w",encoding="utf-8")  #打開一個文件,w是文件不存在則新建一個文件,這里不用wb是因為不用保存成二進制

file.write( response.text )

file.close()

點擊並拖拽以移動

10.爬取圖片,保存到本地

#保存百度圖片到本地

import requests #先導入爬蟲的庫,不然調用不了爬蟲的函數

response = requests.get("https://www.baidu.com/img/baidu_jgylogo3.gif")  #get方法的到圖片響應

file = open("D:\\爬蟲\\baidu_logo.gif","wb") #打開一個文件,wb表示以二進制格式打開一個文件只用於寫入

file.write(response.content) #寫入文件

file.close()#關閉操作,運行完畢后去你的目錄看一眼有沒有保存成功

點擊並拖拽以移動


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM