54

不会这几个库,都不敢说我会Python爬虫

 4 years ago
source link: https://www.tuicool.com/articles/v2q2UrU
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

很多朋友不知道Python爬虫怎么入门,怎么学习,到底要学习哪些内容。今天我来给大家说说学习爬虫,我们必须掌握的一些第三方库。

废话不多说,直接上干货。

V7FnemI.jpg!web

1.请求库

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, 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', '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): 
 async with session.get(url) as response: 
 return await response.text() 
async def main(): 
 async with aiohttp.ClientSession() as session: 
 html = await fetch(session, 'http://python.org') 
 print(html) 
if __name__ == '__main__': 
 loop = asyncio.get_event_loop() 
 loop.run_until_complete(main()) 

2 解析库

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 文档,易用性和解析速度都很好。

3. 数据存储库

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。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK