爬蟲基本知識
爬蟲概念
爬蟲(網絡爬蟲),是一種按照一定規則自動抓取萬維網信息的程序或者腳本。理論上來說,只要是我們在瀏覽器(客戶端)能夠做的事情,爬蟲都可以做。
網頁的特徵
1.每一個網頁都有一個唯一的url(統一資源定位符),來進行定位
2.網頁都是通過HTML(超文本)文本展示的
3.所有的網頁都是通過HTTP<超文本傳輸協議>(HTTPS)協議來傳輸的
爬蟲分類和流程
常用爬蟲主要分爲兩類:
1.通用網絡爬蟲:通常指對搜索引擎的爬蟲。
2.聚焦網絡爬蟲:即爬取某些特定頁面和主題的網絡爬蟲。
爬蟲的基本流程如下圖所示:
1.分析網站,得到目標url
2.根據url,發起請求,獲取頁面的HTML源碼
3.從頁面源碼中提取數據
a.提取到目標數據,做數據的篩選和持久化存儲
b.從頁面中提取到新的url地址,繼續執行第二步操作
4.爬蟲結束:所有的目標url都提取完畢,並且得到數據了,再也沒有其他請求任務了,這時意味着爬蟲結束。
聚焦爬蟲在通用爬蟲的基礎上,增加了對某些特定頁面和主題的定義。在URL的檢索分析過程中保留有用的網頁和信息,然後再將信息保存。
python 3爬蟲常用庫安裝和使用
請求庫
1.requests
安裝方式
windows系統下:
pip install requests
linux系統下:
sudo pip install requests
或者去github上安裝:
https://github.com/requests/requests
導入模塊:
import requests
最常用的方法是requests.get(),具體參數是:
r=requests.get(url,params,**kwargs)
url:爬取的網站地址。
params:參數,url中的額外參數。
**kwargs:12個控制訪問的參數。
僅舉一小例說明:
具體介紹和高級用法可見:
https://requests.kennethreitz.org//zh_CN/latest/user/quickstart.html
https://blog.csdn.net/pittpakk/article/details/81218566
2.selenium(瀏覽器自動化測試框架)
安裝步驟:
1.安裝selenium
Win:
pip install selenium
2.安裝webdriver
各大瀏覽器webdriver地址可參見:https://docs.seleniumhq.org/download/
Firefox:https://github.com/mozilla/geckodriver/releases/
Chrome:https://sites.google.com/a/chromium.org/chromedriver/ 或者
http://chromedriver.storage.googleapis.com/index.html
IE:http://selenium-release.storage.googleapis.com/index.html
注:webdriver需要和對應的瀏覽器版本以及selenium版本對應
3.添加webdriver.exe路徑到環境變量
尤其是出現以下報錯的情況:selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
使用方法(僅舉例用google webdriver打開瀏覽器):
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(5)
driver.close()
執行後應該是:
更詳細介紹可見:
https://www.jianshu.com/p/1531e12f8852
解析庫
Beautiful Soup4(推薦)
安裝
pip install beautifulsoup4
要注意,包名是beautifulsoup4,如果不加上 4,會是老版本也就是 bs3,它是爲了兼容性而存在,目前已不推薦。
常用方法
引用官網的例子:
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
使用 bs 的初始化操作,是用文本創建一個 BeautifulSoup 對象,建議手動指定解析器:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
注:bs在使用的時候需要指定一個”解析器”(解析器不同使用的效果也不同):
html.parse- python 自帶,但容錯性不夠高,對於一些寫得不太規範的網頁會丟失部分內容
lxml- 解析速度快,需額外安裝
xml- 同屬 lxml 庫,支持 XML 文檔
html5lib- 最好的容錯性,但速度稍慢
獲取其中某個結構化元素及其屬性:
soup.title # title 元素
# <title>The Dormouse's story</title>
soup.p # 第一個 p 元素
# <p class="title"><b>The Dormouse's story</b></p>
soup.p['class'] # p 元素的 class 屬性
# ['title']
soup.p.b # p 元素下的 b 元素
# <b>The Dormouse's story</b>
soup.p.parent.name # p 元素的父節點的標籤
# body
其他詳細介紹可參看官方文檔:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html