爬蟲的基本操作
爬蟲基礎知識
什麼是爬蟲?
在最開始,還沒有誕生Google和百度等一系列搜索引擎的公司的時候,人們進入一些公司的網站只能通過在瀏覽器地址欄輸入網址的方式訪問,如同在很早之前前手機不流行的時候,我們會把各個好友的電話號碼抄寫在一個電話本上一樣將各個公司的網站記錄在文檔中,很不方便。
當搜索引擎公司出現的時候,這些搜索引擎公司來做了一個大黃頁,把所有網站的網址蒐集起來,用戶不用和各個公司打交道,而是直接和搜索引擎公司打交道,讓搜索引擎幫助自己在它自己製作的大黃頁中找出用戶需要的內容返回給用戶使用。簡單點說,就是公司把信息交給搜索引擎公司,讓搜索引擎公司在有用戶需要本公司信息的時候提交給用戶。
搜索引擎公司蒐集數據不光是公司或個人主動上傳,主要還會通過網絡爬蟲爬去網上信息並且提取出關鍵字更網友搜索。
整個網絡可以理解爲通過a標籤鏈接起來的蜘蛛網,可以在網絡中獲取任何信息。
爬蟲的作用就是蒐集網絡信息爲我所用。
爬蟲的分類
定向爬蟲:只爬取某一個或某幾個網站,根據自己的需要有專一目的性爬取。
非定向爬蟲:隨機爬去整個網絡的網站,見什麼爬什麼。
爬蟲爬取大體步驟
假設我們爬取“汽車之家”的相關數據:https://www.autohome.com.cn/news/
下載頁面:
請求網站:https://www.autohome.com.cn/news/ ,返回的爲 HTML 頁面字符串。
篩選信息:
使用正則表達式篩選出我們需要的信息。(python非常牛逼的是有大神把正則表達式寫好了,都在開源模塊裏邊了。)
開源模塊使用的簡單案例
# __author : "王佳偉"
# date : 2019-01-16
import requests
from bs4 import BeautifulSoup
# 1.request 模塊
respone = requests.get("https://www.autohome.com.cn/news/")
respone.text
# 2.BeautifulSoup 模塊
soup = BeautifulSoup(respone.text, features='html.parser') # 把HTML文本轉換爲對象
target = soup.find(id='auto-channel-lazyload-article')
print(target)
爬取汽車之家數據案例
爬取汽車之家新聞列表的標題即鏈接
# __author : "王佳偉"
# date : 2019-01-16
import requests
from bs4 import BeautifulSoup
# 1.request 模塊
# 下載目標網站的HTML源碼
response = requests.get(
url="https://www.autohome.com.cn/news/"
)
# 編碼查看網頁頭
# respone.encoding='gb2312'
# 拿到文本並轉換爲網頁自己的編碼
response.encoding = response.apparent_encoding
# print(response.text)
# 2.BeautifulSoup 模塊
# 把HTML文本轉換爲對象,features表示以什麼引擎處理 html.parser / lxml
soup = BeautifulSoup(response.text, features='html.parser')
# 根據id屬性找,找到對象中 id = auto-channel-lazyload-article 的標籤
target = soup.find(id='auto-channel-lazyload-article')
# 打印輸出
# print(target)
# 根據標籤找 找到所有標籤爲li的所有標籤
li_list = target.find_all('li')
# print(obj)
# 循環列表
for i in li_list:
# 找到每個li標籤中包含的a標籤
# a = i.find('a')
# print(a)
a = i.find('a')
if a:
# print(a.attrs) # 找到a標籤的所有屬性
print(a.attrs.get('href')) # 找到a標籤的href屬性
txt = a.find('h3') # 獲取a標籤中的h3標籤
print(txt)
# print(txt.text)
爬取下載汽車之家新聞列表的圖片
# __author : "王佳偉"
# date : 2019-01-16
import requests
from bs4 import BeautifulSoup
# 1.request 模塊
# 下載目標網站的HTML源碼
response = requests.get(
url="https://www.autohome.com.cn/news/"
)
# 編碼查看網頁頭
# respone.encoding='gb2312'
# 拿到文本並轉換爲網頁自己的編碼
response.encoding = response.apparent_encoding
# print(response.text)
# 2.BeautifulSoup 模塊
# 把HTML文本轉換爲對象,features表示以什麼引擎處理 html.parser / lxml
soup = BeautifulSoup(response.text, features='html.parser')
# 根據id屬性找,找到對象中 id = auto-channel-lazyload-article 的標籤
target = soup.find(id='auto-channel-lazyload-article')
# 打印輸出
# print(target)
# 根據標籤找 找到所有標籤爲li的所有標籤
li_list = target.find_all('li')
# print(obj)
# 循環列表
for i in li_list:
# 找到每個li標籤中包含的a標籤
# a = i.find('a')
# print(a)
a = i.find('a')
if a:
# print(a.attrs) # 找到a標籤的所有屬性
# print(a.attrs.get('href')) # 找到a標籤的href屬性
txt = a.find('h3') # 獲取a標籤中的h3標籤
# print(txt)
print(txt.text)
# 找a標籤中的img標籤的src屬性
img_url = a.find('img').attrs.get('src')
print(img_url)
# 下載圖片
img_response = requests.get(url='https:'+img_url)
# 設置文件名
import uuid
# 隨機生成文件名
file_name = str(uuid.uuid4()) + '.jpg'
with open(file_name,'wb') as f:
f.write(img_response.content)
總結
requests 模塊的使用
# 導包
import requests
# 爬取那個網站,填寫URL地址
response = requests.get('URL')
# 獲取對象的文本內容
response.text
# 獲取圖片/視頻內容
response.content
# 設置編碼
response.encoding
# 獲取網站自己的編碼類型
response.apparent_encoding
# 獲取狀態碼
response.status_code
BeautifulSoup 模塊的使用
# 導包
from bs4 import BeautifulSoup
# 獲取對象
soup = BeautifulSoup('<html>......</html>', features='html.paeser')
# 找到它孩子第一個符合條件的第一個div
v1 = soup.find('div')
# 找它孩子中第一個 id = d1 的標籤
v1 = soup.find(id='d1')
# 找到它孩子第一個id=d1的div
v1 = soup.find('div', id='d1')
# 用法同find,找所有,返回值爲列表
v2 = soup.find_all('div')
v2 = soup.find_all(id='d1')
v2 = soup.find_all('div', id='d1')
obj = v1
obj = v2[0]
# 獲取值
obj.text
# 獲取屬性
obj.attrs
# 獲取屬性值
obj.attrs.get('屬性')
完成!