1.前言
一般的爬虫的是用python脚本直接对目标网站进行访问,访问速度很快,这样目标网站很容易就识别出你是机器人。而使用selenium写爬虫,python脚本操控浏览器进行访问,这样的行为更像是人类行为,这样很多难爬的网站也可以轻而易举的抓数据了。
2.准备
(1)Chrome用户
Chrome driver下载地址:
http://chromedriver.storage.googleapis.com/index.html
chrome和chrome driver对应版本:
chromedriver版本 | 支持的Chrome版本 |
---|---|
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
v2.14 | v39-42 |
v2.13 | v38-41 |
v2.12 | v36-40 |
v2.11 | v36-40 |
v2.10 | v33-36 |
v2.9 | v31-34 |
v2.8 | v30-33 |
v2.7 | v30-33 |
v2.6 | v29-32 |
v2.5 | v29-32 |
v2.4 | v29-32 |
(2)Firefox用户
安装firebug,下载网址为https://addons.mozilla.org/zh-CN/firefox/addon/firebug/
再安装firepath,下载网址为https://addons.mozilla.org/zh-CN/firefox/addon/firepath/
selenium库从3.x开始,需要额外安装geckodriver下载网址为https://github.com/mozilla/geckodriver/releases下载解压后放在任何地方并将其路径加入path,个人推荐放在python的目录,这样省去了配置path的麻烦
3.基本使用
导包
from selenium import webdriver
from bs4 import BeautifulSoup
初始化浏览器
driver = webdriver.Firefox()
打开某个网址
driver.get(url)
如果网站需要输入登录账号密码
这里用到firepath找到目标位置的xpath
找到输入账号框,清除框内信息,再输入你的账号
driver.find_element_by_xpath(xpath).clear()
driver.find_element_by_xpath(xpath).send_keys("你的账号")
找到输入密码框,清除框内信息,再输入你的密码
driver.find_element_by_xpath(xpath).clear()
driver.find_element_by_xpath(xpath).send_keys("你的密码")
定位“点击登录”框的位置的xpath,执行登录
driver.find_element_by_xpath(xpath).click()
获取该网页的源码
html = driver.page_source
BeautifulSoup定位标签
bsObj = BeautifulSoup(html,‘html.parser’)
再之后如何操作和普通爬虫一样了。
4.获取xpath
前提,确保已经安装好firefox,并且安装好插件firebug和firepath。
首先,点击图中1,打开firebug
然后,点击图中2,打开firepath
再然后,点击图中的3,箭头
再然后,点击目标位置,例如图中的4为账号输入框
最后,在图中5复制xpath的值
5.示例代码
from selenium import webdriver
from bs4 import BeautifulSoup
def getHTMLText(url):
driver = webdriver.Firefox()
driver.get(url)
userNameXpath = ".//*[@id='UserName']"
driver.find_element_by_xpath(userNameXpath).clear()
driver.find_element_by_xpath(userNameXpath).send_keys('替换为你的用户名')
pasWrdXpath = ".//*[@id='Password']"
driver.find_element_by_xpath(pasWrdXpath).clear()
driver.find_element_by_xpath(pasWrdXpath).send_keys('替换为你的密码')
loginXpath = ".//*[@id='main']/div[2]/div/form/div/fieldset/div[4]/input"
driver.find_element_by_xpath(loginXpath).click()
html = diver.page_source
return html
def main():
url = 'http://www.ituring.com.cn/account/logon'
html = getHTMLText(url)
main()
运行后你就能看见爬虫自动打开浏览器并登陆账户啦。当前程序还不能处理验证码的操作,所以实验时选取没有验证码的网站。关于增加验证码的代码,将在后续补充。