python爬虫工具安装

re与urllib

re与urllib模块一般python自带

requests模块安装

pip install requests

selenium

有些网站是js渲染的,需要借助浏览器加载,在此需要安装selenium

pip install selenium

chromedrive

还需要安装chrome浏览器以及浏览器对应的chromedrive
安装成功测试:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://weibo.com/p/1004061634785555/home?is_search=0&visible=0&is_all=1&is_tag=0&profile_ftype=1&page=1#feedtop")

这里写图片描述

phantomjs

可以看出以上需要打开一个网页,也可以使用phantomjs,运行时不需要打开网了,即可在后台运行。

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get("baidu.com")

也不得不说PhantomJS里面的坑很多,要想轻松点,上一个选择就可以了。

lxml

提供xpath的高效解析方式

pip install lxml

beautifulsoup4

beautifulsoup4也是一个网页解析库,使用方便,依赖xlml,安装之前先安装lxml。

pip install beautifulsoup4

调用方式

from bs4 import BeautifulSoup
soup = BeautifulSoup('<html></html>','lxml') # 使用xlml解析

pyquery

网页解析库,相对bs4是要方便的,语法跟jQuery是相似的。

from pyquery import PyQuery as pq
doc = pq("<html>hello</html>")
result = doc("html").text()
result
# 'hello'