1. 程式人生 > 程式設計 >不會Python的這幾個庫,我都不敢說會爬蟲

不會Python的這幾個庫,我都不敢說會爬蟲

來源:JAVAandPython君

☞ 分享:最全最新的Python學習大禮包 ☜

很多朋友不知道Python爬蟲怎麼入門,怎麼學習,到底要學習哪些內容。今天我來給大家說說學習爬蟲,我們必須掌握的一些第三方庫。

廢話不多說,直接上乾貨。

請求庫

1. requests

GitHub:https://github.com/psf/requests

requests庫應該是現在做爬蟲最火最實用的庫了,非常的人性化。有關於它的使用我之前也寫過一篇文章 一起看看Python之Requests庫 ,大家可以去看一下。

有關於requests最詳細的使用方法,大家可以參考官方檔案:https://requests.readthedocs.io/en/master/

小案例

>>> import requests
>>> r = requests.get('https://api.github.com/user', auth=('user''pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'disk_usage'
368627,112); word-wrap: inherit !important; word-break: inherit !important;" class="hljs-string">u'private_gists': 484, ...}
複製程式碼

2.urllib3

GitHub:https://github.com/urllib3/urllib3

urllib3是一個非常強大的http請求庫,提供一系列的操作URL的功能。

有關於它的詳細使用方法可以參考:https://urllib3.readthedocs.io/en/latest/

使用小案例:

import urllib3
>>> http = urllib3.PoolManager()
>>> r = http.request('GET'
,112); word-wrap: inherit !important; word-break: inherit !important;" class="hljs-string">'http://httpbin.org/robots.txt')
>>> r.status
200
>>> r.data
'User-agent: *\nDisallow: /deny\n'
複製程式碼

3.selenium

GitHub:https://github.com/SeleniumHQ/selenium

自動化測試工具。一個呼叫瀏覽器的 driver,通過這個庫你可以直接呼叫瀏覽器完成某些操作,比如輸入驗證碼。

對於這個庫並非只是Python才能用,像JAVA、Python、C#等都能夠使用selenium這個庫

有關於Python語言如何去使用這個庫,大家可以去訪問https://seleniumhq.github.io/selenium/docs/api/py/ 檢視官方檔案

使用小案例:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
複製程式碼

4.aiohttp

GitHub:https://github.com/aio-libs/aiohttp

基於 asyncio 實現的 HTTP 框架。非同步操作藉助於 async/await 關鍵字,使用非同步庫進行資料抓取,可以大大提高效率。

這個屬於進階爬蟲時候必須掌握的非同步庫。有關於aiohttp的詳細操作,可以去官方檔案:https://aiohttp.readthedocs.io/en/stable/

使用小案例:

import aiohttp
import asyncio

async def fetch(session, url):
    with session.get(url) as response:
        return await response.text()

main():
    with aiohttp.ClientSession() as session:
        html = await fetch(session,112); word-wrap: inherit !important; word-break: inherit !important;" class="hljs-string">'http://python.org')
        print(html)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
複製程式碼

解析庫

1、beautifulsoup

官方檔案:https://www.crummy.com/software/BeautifulSoup/

html 和 XML 的解析,從網頁中提取資訊,同時擁有強大的API和多樣解析方式。一個我經常使用的解析庫,對於html的解析是非常的好用。對於寫爬蟲的人來說這也是必須掌握的庫。

2、lxml

GitHub:https://github.com/lxml/lxml

支援HTML和XML的解析,支援XPath解析方式,而且解析效率非常高。

3、pyquery

GitHub:https://github.com/gawel/pyquery

jQuery 的 Python 實現,能夠以 jQuery 的語法來操作解析 HTML 檔案,易用性和解析速度都很好。

資料儲存

1、pymysql

GitHub:https://github.com/PyMySQL/PyMySQL

官方檔案:https://pymysql.readthedocs.io/en/latest/

一個純 Python 實現的 MySQL 客戶端操作庫。非常的實用、非常的簡單。

2、pymongo

GitHub:https://github.com/mongodb/mongo-python-driver

官方檔案:https://api.mongodb.com/python/

顧名思義,一個用於直接連線 mongodb 資料庫進行查詢操作的庫。

3、redisdump

使用方法:https://blog.csdn.net/zhwitbird/article/details/81279406

redis-dump是將redis和json互轉的工具;redis-dump是基於ruby開發,需要ruby環境,而且新版本的redis-dump要求2.2.2以上的ruby版本,centos中yum只能安裝2.0版本的ruby。需要先安裝ruby的管理工具rvm安裝高版本的ruby;