输入关键字 抓取所有网页(如何安装selenium?1.输入命令installselenium2. )
优采云 发布时间: 2022-01-23 20:03输入关键字 抓取所有网页(如何安装selenium?1.输入命令installselenium2.
)
文章目录
反向爬虫11 selenium基础一、什么是selenium?
Selenium 是一种自动化测试工具。您可以启动一个全新的浏览器并从浏览器中提取您想要的内容。
二、为什么要学习硒?
学习了这样一个requests模块,已经可以拿到网页的源码了。为什么要学习 selenium,一个不适合爬虫的自动化测试工具?因为现在很多网站对数据进行加密,然后通过javascript对数据进行解密,requests模块只能获取到加密后的数据,而之前学到的知识已经无法爬取网站这样的数据,selenium模块可以提供浏览器环境,浏览器会加载javascript代码解密数据,然后通过selenium提取目标内容,所以selenium可以处理大部分数据加密情况(大厂商除外)。
三、如何安装硒?1. 输入命令 pip install selenium 2. 下载浏览器驱动
3. 下载后放到python解释器目录下
四、硒怎么用?1. 打开浏览器,输入网址回车
from selenium.webdriver import Chrome
web = Chrome() # 此时自动查找浏览器驱动
url = "http://www.baidu.com"
web.get(url)
print(web.title) # 固定的. 获取到网站的titile标签中的内容
2. 硒的各种神奇操作
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
web = Chrome()
url = "https://shanghai.zbj.com/"
web.get(url)
time.sleep(1)
# 点击外包需求
print("选择外包需求")
btn = web.find_element(By.XPATH, '//*[@id="utopiacs-zp-header-v1"]/div/div/div[3]/div[3]/div[1]/a/span')
btn.click()
time.sleep(1)
# 切换窗口
print("切换窗口")
web.switch_to.window(web.window_handles[-1]) # 跳转到最后一个窗口
# 关闭广告
print("正在关闭广告")
web.execute_script("""
var a = document.getElementsByClassName("hall-top-xw")[0];
a.parentNode.removeChild(a);
""")
# 选择输入框,输入python
print("输入python,点击搜索")
web.find_element(By.XPATH, '//*[@id="utopia_widget_2"]/div/div[2]/div/input').send_keys("python", Keys.ENTER)
time.sleep(1)
# 切换窗口
print("切换窗口")
web.switch_to.window(web.window_handles[-1]) # 跳转到最后一个窗口
# 获取任务信息和赏金
print("获取任务信息和赏金")
for i in range(2):
# 关闭广告
print("正在关闭广告")
web.execute_script("""
var a = document.getElementsByClassName("hall-top-xw")[0];
a.parentNode.removeChild(a);
""")
div_list = web.find_elements(By.XPATH, '//*[@id="utopia_widget_6"]/div/div[1]/div')
for div in div_list:
name = div.find_element(By.XPATH, './div[1]/h4/a').text
detail = div.find_element(By.XPATH, './div[2]').text
salary = div.find_element(By.XPATH, './div[4]/span').text
print(name, detail, salary)
next = web.find_element(By.XPATH, '//*[@id="utopia_widget_8"]/a[9]')
next.click()
time.sleep(1)
print("关闭当前窗口")
web.close()
print("切换回第一个窗口")
web.switch_to.window(web.window_handles[0])
time.sleep(1)
3. 如何在 iframe 中获取内容
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time
web = Chrome()
web.get("http://www.wbdy.tv/play/30288_1_1.html")
time.sleep(5)
# 切换iframe
iframe = web.find_element(By.XPATH, '//*[@id="mplay"]')
web.switch_to.frame(iframe)
# 获取标签属性
input = web.find_element(By.XPATH, '//*[@id="dplayer"]/div[4]/div[1]/input')
placeholder = input.get_property("placeholder")
print(placeholder)
# 跳出iframe
web.switch_to.parent_frame()
content = web.find_element(By.XPATH, '/html/body/div[2]/div[3]/div[2]/div/div[2]')
print(content.text)
4. 下拉列表切换,取页面代码(非源码)
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome
from selenium.webdriver.support.select import Select # 下拉列表
import time
web = Chrome()
web.get("https://www.endata.com.cn/BoxOffice/BO/Year/index.html")
sel = web.find_element(By.XPATH, '//*[@id="OptionDate"]')
sel_new = Select(sel)
# selenium可以一口气拿到标签和其后代标签中的文本内容,因此直接拿表格标签,打印它的text
for i in range(len(sel_new.options)):
sel_new.select_by_index(i) # 根据位置切换
time.sleep(3)
div = web.find_element(By.XPATH, '//*[@id="TableList"]/table/tbody')
print(div.text)
# 获取页面代码( 不是页面源代码, 是F12里面 elements的代码)
page_source = web.page_source
print(page_source)
5. 隐藏浏览器
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome
from selenium.webdriver.support.select import Select
import time
# 配置无头信息
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")
web = Chrome(options=opt)
web.get("https://www.endata.com.cn/BoxOffice/BO/Year/index.html")
sel = web.find_element(By.XPATH, '//*[@id="OptionDate"]')
sel_new = Select(sel)
for i in range(len(sel_new.options)):
sel_new.select_by_index(i)
time.sleep(3)
div = web.find_element(By.XPATH, '//*[@id="TableList"]/table/tbody')
print(div.text)
# 获取页面代码( 不是页面源代码, 是F12里面 elements的代码)
page_source = web.page_source
print(page_source)
五、如何获取验证码1. 为什么会有验证码?
验证码最初是一种人机验证方法,旨在防止暴力破解密码。银行密码一般为 6 位,共有 10 的 6 次方和 100 万种可能性。如果有人知道了你的银行卡号,写了一个详尽的代码,一次又一次地访问银行网站,那么他最多可以登录你的银行账户100万次,这对于计算机来说并不是一件难事程序。因此,人们设计了一种验证码。每次登录时,都会要求您手动识别验证码中的内容并输入。验证通过后,即可登录访问。加入此验证码机制后,普通穷举码无法破解密码。
2.使用超级鹰破解验证码注册超级鹰账号,充值(超级鹰每次识别验证码都会消耗积分),进入用户中心,生成软件ID,复制软件ID下载样本码,并把超鹰账号、密码、软件ID替换,运行程序得到样本验证码图片的识别结果
该过程不截屏。具体使用方法可以阅读官方文档。代码贴在下面。
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class Chaojiying_Client(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
if __name__ == '__main__':
chaojiying = Chaojiying_Client('xxxxxx', 'xxxxxx', '96001') #用户中心>>软件ID 生成一个替换 96001
im = open('a.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 1902)) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
3. 使用超级鹰获得超级鹰
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
web = Chrome()
web.get("http://www.chaojiying.com/user/login/")
png = web.find_element(By.XPATH, '/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png
chaojiying = Chaojiying_Client('xxxxxx', 'xxxxxx', 'xxxxxx') #用户中心>>软件ID 生成一个替换 96001
result = chaojiying.PostPic(png, 1902) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
v_code = result['pic_str']
web.find_element(By.XPATH, '/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys("18614075987")
web.find_element(By.XPATH, '/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys("q6035945")
web.find_element(By.XPATH, '/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(v_code)
web.find_element(By.XPATH, '/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input').click()