selenium,获取cookie,xpath,超级鹰,京东,12306爬取

selenium使用-->可以使用代码控制模拟人操作浏览器

# 如果使用requests模块,发送请求获取的数据不全,它不能执行js

# selenium:可以使用代码控制模拟人操作浏览器


## 操作某个浏览器,就需要有浏览器驱动
# http://npm.taobao.org/mirrors/chromedriver/  谷歌驱动的淘宝镜像站
# 注意
# 谷歌浏览器版本要跟驱动版本对应
 
## 92.0.4515.131  下载相应版本驱动,放到项目代码中

# pip3 install selenium

# from selenium import webdriver   导入模块
# import time
# # 打开一个谷歌浏览器
# bro=webdriver.Chrome(executable_path='chromedriver.exe')
#
# #地址栏中输入百度
# bro.get('https://www.cnblogs.com/')   # 输入指定的url地址
#
# time.sleep(2)
#
# print(bro.page_source)  # 当前页面的html内容
#
# bro.close()  # 关闭浏览器

selenium操作浏览器

import time

from selenium import webdriver

"""
webdriver指定浏览器
executable_path指执行文件路径。
"""
# 这行代码表示打开了谷歌浏览器
bro = webdriver.Chrome(executable_path='./chromedriver')
"""
隐性等待设置了一个时间,在一段时间内网页是否加载完成,
如果完成了,就进行下一步;在设置的时间内没有加载完成,则会报超时加载。
在开头设置过之后,整个的程序运行过程中都会有效,都会等待页面加载完成;不需要每次设置一遍;
"""
bro.implicitly_wait(10)
# 在地址栏中输入url
bro.get('http://www.baidu.com')
# 拿到当前页面的html内容
# print(bro.page_source)
# 找到登录标签的id
login_path = bro.find_element_by_id('s-top-loginbtn')
# 点击
login_path.click()
# 找到用户名和密码输入框
username = bro.find_element_by_id('TANGRAM__PSP_11__userName')
password = bro.find_element_by_id('TANGRAM__PSP_11__password')

# 在输入框中写东西
username.send_keys('1348410743@qq.com')
password.send_keys('11111')

# 找到登录按钮
baidu_login = bro.find_element_by_id('TANGRAM__PSP_11__submit')
baidu_login.click()

# 程序等待2秒再关闭
time.sleep(2)
# 关闭浏览器
bro.close()

1

无头浏览器

后台爬取,不显示界面。

from selenium import webdriver

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败


driver=webdriver.Chrome(executable_path='chromedriver.exe',chrome_options=chrome_options)
driver.get('https://www.baidu.com')
print(driver.page_source)
driver.close()

获取元素位置,属性,大小

from selenium import webdriver
import time
driver=webdriver.Chrome(executable_path='chromedriver.exe')
driver.get('https://kyfw.12306.cn/otn/resources/login.html')
driver.implicitly_wait(10)

user_login=driver.find_element_by_css_selector('.login-hd-account>a')

user_login.click()
time.sleep(2)
img=driver.find_element_by_id('J-loginImg')
print(img)

print(img.id)    #selenium提供的id,忽略
print(img.tag_name) # 标签名

print('-----')
print(img.location) # img标签的位置 
print(img.size)     # img标签大小

# 获取属性
# print(img.get_attribute('src'))
print(img.get_attribute('class'))

driver.close()

等待元素被加载

两种等待方式:

  • 显示等待
  • 隐形等待 --> 推荐使用
from selenium import webdriver

driver=webdriver.Chrome(executable_path='chromedriver.exe')
driver.get('https://www.baidu.com')
'''
# 两种等待方式
# 显示等待(忽略掉)
    wait=WebDriverWait(driver,10)
    wait.until(EC.presence_of_element_located((By.ID,'content_left')))
    contents=browser.find_element(By.CSS_SELECTOR,'#content_left')
# 隐式等待:
    -driver.implicitly_wait(10)
    -driver.find_element_by_css_selector()
    -只需要写一句话,等待所有要获取的标签
'''

driver.implicitly_wait(10)


print(driver.page_source)
# 再找控件,只要没加载成功,就会等待,最多等10s
driver.close()

元素操作

  • 输入:send_keys('内容')
  • 清除:.clear( )
  • 点击:click( )
import time
from selenium import webdriver

bro = webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://www.baidu.com')
bro.implicitly_wait(10)

# 点击操作,点击百度搜索框
input_bro = bro.find_element_by_id('kw')
# 输入我需要搜索的信息
input_bro.send_keys('python')
time.sleep(2)
# 清除输入框中的python信息
input_bro.clear()
# 输入我需要百度的关键字
input_bro.send_keys('黑丝')
time.sleep(2)
# 找到百度一下的点击按钮
baidu_button = bro.find_element_by_id('su')
baidu_button.click()

执行js代码

from selenium import webdriver
import time

driver=webdriver.Chrome(executable_path='chromedriver.exe')
driver.get('http://127.0.0.1:8000/')
driver.implicitly_wait(10)

# 可以写的很高级
driver.execute_script("name='egon';") # 这里面写js代码
driver.execute_script("alert(name)") # 这里面写js代码


time.sleep(5)
# driver.close()

切换选项卡即标签页-->switch_to.window

import time
from selenium import webdriver

browser=webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')

print(browser.window_handles) #获取所有的选项卡
# browser.switch_to_window(browser.window_handles[1])
# browser.switch_to_window(browser.window_handles[1])
browser.switch_to.window(browser.window_handles[1])
browser.get('https://www.taobao.com')
time.sleep(5)
# browser.switch_to_window(browser.window_handles[0])
browser.switch_to.window(browser.window_handles[0])
browser.get('https://www.sina.com.cn')
browser.close()

模拟前进后退

import time
from selenium import webdriver

browser=webdriver.Chrome(executable_path='chromedriver.exe')
browser.get('https://www.baidu.com')
browser.get('https://www.taobao.com')
browser.get('http://www.sina.com.cn/')

browser.back()
time.sleep(3)
browser.forward()
browser.close()

异常处理


from selenium import webdriver
from selenium.common.exceptions import TimeoutException,NoSuchElementException,NoSuchFrameException

browser = webdriver.Chrome()
try:
    browser.get('http://www.baidu.com')

except Exception as e:
    print(e)
finally:
    browser.close()

elenium登录cnblogs获取cookie

注意:一定要清楚获取到cookie的当前页面。不同页面的cookie是不同的。

import json
import time
from selenium import webdriver

bro = webdriver.Chrome(executable_path='./chromedriver')
bro.implicitly_wait(5)
# 登录,并获取cookies
bro.get('https://www.cnblogs.com/')
login_button = bro.find_element_by_xpath('//*[@id="navbar_login_status"]/a[6]')
login_button.click()
username = bro.find_element_by_id('mat-input-0')
username.send_keys('1348410743@qq.com')
password = bro.find_element_by_id('mat-input-1')
password.send_keys('xJNN0315')
real_login = bro.find_element_by_xpath(
    '/html/body/app-root/app-sign-in-layout/div/div/app-sign-in/app-content-container/div/div/div/form/div/button/span[1]')
real_login.click()
# get_cookies() 获取cookies
# 睡10s也是一样的,不用刷新
time.sleep(10)
"""
在这里刷新是为了获取到博客园主页面的cookie
下面使用cookie登录时的页面也是主页面
"""
bro.refresh()
cookies = bro.get_cookies()
with open('cookie.json', 'w') as f:
    json.dump(cookies, f)

# 退出登录状态,使用cookie登录
bro.get('https://www.cnblogs.com/')
with open('cookie.json','r')as f:
    cookie = json.load(f)
time.sleep(2)
for item in cookie:
    print(item)
    bro.add_cookie(item)

# 刷新当前页面
bro.refresh()

抽屉半自动点赞



from selenium import webdriver
import json
import time

#### 登录过程
# bro=webdriver.Chrome(executable_path='chromedriver.exe')
# bro.implicitly_wait(10)
# bro.get('https://dig.chouti.com/')
# try:
#     sub_btn=bro.find_element_by_id('login_btn')
#     print(sub_btn)
#
#     # sub_btn.click()  # 报错
#     bro.execute_script('arguments[0].click();',sub_btn)
#
#     # username=bro.find_element_by_css_selector('body > div.login-dialog.dialog.animated2.scaleIn > div > div.login-body > div.form-item.login-item.clearfix.phone-item.mt24 > div.input-item.input-item-short.left.clearfix > input')
#     username=bro.find_element_by_css_selector('div.input-item>input.login-phone')
#     username.send_keys('18953675221')
#     # password=bro.find_element_by_css_selector('body > div.login-dialog.dialog.animated2.scaleIn > div > div.login-footer > div.form-item.login-item.clearfix.mt24 > div')
#     password = bro.find_element_by_css_selector('div.input-item>input.pwd-password-input')
#     password.send_keys('lqz123')
#
#     time.sleep(3)
#     btn=bro.find_element_by_css_selector('body > div.login-dialog.dialog.animated2.scaleIn > div > div.login-footer > div:nth-child(4) > button')
#
#     btn.click()
#
#     input('等')
#
#     with open('chouti.json','w') as f:
#         json.dump(bro.get_cookies(),f)
#
#
#
#
# finally:
#     bro.close()
import requests

bro=webdriver.Chrome(executable_path='chromedriver.exe')
bro.implicitly_wait(10)
bro.get('https://dig.chouti.com/')



# 把屏幕滑倒最底下
bro.execute_script('window.scrollTo(0, document.body.scrollHeight);')
# bro.find_elements_by_css_selector('.link-item')
cookie={}
##从文件中读出cookie
with open('chouti.json','r') as f:
    res=json.load(f)
for item in res:
    cookie[item['name']]=item['value']

print(cookie) # requests能够使用的cookie


div= bro.find_element_by_class_name('link-con')
time.sleep(2)
header={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
}
div_list=div.find_elements_by_class_name('link-item')
for div in div_list:
    article_id=div.get_attribute('data-id')
    print(article_id)
    # 使用requests发送请求
    res=requests.post('https://dig.chouti.com/link/vote',data={'linkId': article_id},cookies=cookie,headers=header)
    print(res.text)
bro.close()

打码平台使用 --》超级鹰使用

# 人工破解
# 图像识别模块---》数字,字母组合
# 验证码破解平台---》云打码,超级鹰
	-给它一张图片---》结果返回   (收费的)

#!/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('306334678', 'lqz12345', '903641')  # 用户中心>>软件ID 生成一个替换 96001
    im = open('a.jpg', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    print(chaojiying.PostPic(im, 1902))  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()

xpath使用

1 一门在html中查找数据的语言
2 记住的语法:
	/   取当前路径下的xx   
    //  取所有路径下的xx   
    .   当前路径    
    ..   上一层
	@    取属性
    
4 lxml解析模块提供的xpath
doc='''
<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html' name='sss'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html' name='lqz'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html' class='li'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
   <a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
  </div>
 </body>
</html>
'''
from lxml import etree

# 传入要解析的内容
html=etree.HTML(doc)

# res=html.xpath('//body')
# print(res)

# 1 所有节点
# a=html.xpath('//*')




# 2 指定节点(结果为列表)
# a=html.xpath('//head')
# 3 子节点,子孙节点
# a=html.xpath('//div/a')
# a=html.xpath('//body//a') #无数据
# a=html.xpath('//body//a')
# 4 父节点
# a=html.xpath('//body//a[@href="image1.html"]/..')
# a=html.xpath('//body//a')
# a=html.xpath('//body//a[@href="image1.html"]')
# a=html.xpath('//body//a[1]/..')
# 也可以这样
# a=html.xpath('//body//a[1]/parent::*')
# a=html.xpath('//body//a[1]/parent::p')
# 5 属性匹配
# a=html.xpath('//a[@href="image1.html"]')
# a=html.xpath('//a[@name="sss"]')

# 6 文本获取  text()
# a=html.xpath('//a[@href="image1.html"]/text()')
# a=html.xpath('//a/text()')

# 7 属性获取
# a=html.xpath('//a/@href')
# a=html.xpath('//a[1]/@name')
# # 注意从1 开始取(不是从0)
# a=html.xpath('//body//a[2]/@href')
# 8 属性多值匹配
#  a 标签有多个class类,直接匹配就不可以了,需要用contains
# a=html.xpath('//a[@class="li"]')
# a=html.xpath('//a[contains(@class,"li")]')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 9 多属性匹配
# a=html.xpath('//body//a[contains(@class,"li") or @name="items"]')
# a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 10 按序选择
# a=html.xpath('//a[2]/text()')
# a=html.xpath('//a[2]/@href')
# a=html.xpath('//a[2]/@name')
# 取最后一个
# a=html.xpath('//a[last()]/@href')
# 位置小于3的
# a=html.xpath('//a[position()<3]/@href')
# 倒数第二个
# a=html.xpath('//a[last()-2]/@href')
# 11 节点轴选择
# ancestor:祖先节点
# 使用了* 获取所有祖先节点
# a=html.xpath('//a/ancestor::*')
# # 获取祖先节点中的div
# a=html.xpath('//a/ancestor::div')
# attribute:属性值
# a=html.xpath('//a[1]/attribute::*')
# child:直接子节点
# a=html.xpath('//a[1]/child::*')
# a=html.xpath('//a[1]/child::img/@src')
# descendant:所有子孙节点
# a=html.xpath('//a[6]/descendant::*')
# following:当前节点之后所有节点
# a=html.xpath('//a[1]/following::*')
# a=html.xpath('//a[1]/following::*[1]/@href')
# following-sibling:当前节点之后同级节点
# a=html.xpath('//a[1]/following-sibling::*')
# a=html.xpath('//a[1]/following-sibling::a')
# a=html.xpath('//a[1]/following-sibling::*[2]/text()')
# a=html.xpath('//a[1]/following-sibling::*[2]/@href')

print(a)

爬取京东商品信息

抠图使用超级鹰破解验证码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys  # 键盘按键操作
import time


def get_goods(driver):
    try:
        # 找到所有类名叫gl-item的标签
        goods = driver.find_elements_by_class_name('gl-item')
        for good in goods:
            detail_url = good.find_element_by_tag_name('a').get_attribute('href')
            p_name = good.find_element_by_css_selector('.p-name em').text.replace('\n', '')
            price = good.find_element_by_css_selector('.p-price i').text
            p_commit = good.find_element_by_css_selector('.p-commit a').text
            img=good.find_element_by_css_selector('div.p-img img').get_attribute('src')
            if not img:
                img='http:'+good.find_element_by_css_selector('div.p-img img').get_attribute('data-lazy-img')
            msg = '''
                商品 : %s
                链接 : %s
                图片 : %s
                价钱 :%s
                评论 :%s
                ''' % (p_name, detail_url,img, price, p_commit)

            print(msg, end='\n\n')

        button = driver.find_element_by_partial_link_text('下一页')
        button.click()
        time.sleep(1)
        get_goods(driver)
    except Exception:
        pass


def spider(url, keyword):
    driver = webdriver.Chrome(executable_path='./chromedriver')
    driver.get(url)
    driver.implicitly_wait(3)  # 使用隐式等待
    try:
        input_tag = driver.find_element_by_id('key')
        input_tag.send_keys(keyword)
        input_tag.send_keys(Keys.ENTER) # 敲回车
        get_goods(driver)
    finally:
        driver.close()


if __name__ == '__main__':
    spider('https://www.jd.com/', keyword='精品内衣')

12306软件自动登录


from selenium import webdriver
import base64
from PIL import Image
import time
from chaojiying import Chaojiying_Client
from selenium.webdriver import ActionChains


# 不让程序检测出是用驱动控制
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")
bro=webdriver.Chrome(executable_path='./chromedriver.exe',chrome_options=options)

bro.get('https://kyfw.12306.cn/otn/resources/login.html')

bro.implicitly_wait(10)
# 把窗口设置全屏
bro.maximize_window()

try:
    username_login_btn=bro.find_element_by_css_selector('.login-hd-account>a')
    username_login_btn.click()

    username=bro.find_element_by_id('J-userName')
    password=bro.find_element_by_id('J-password')
    login_btn=bro.find_element_by_id('J-login')
    username.send_keys('liuqingzheng')
    password.send_keys('lqz12345')


    img_code=bro.find_element_by_id('J-loginImg')
    print(img_code.size)
    print(img_code.location)
    # 获取验证码图片的两种方案
    # 方案一:整体截图,根据位置抠出验证码图片
    # bro.save_screenshot('main.png') # 对整个页面进行截图,main.png
    #
    # location=img_code.location
    # size=img_code.size
    # print(location)
    # print(size)
    # #验证码的坐标
    # img_tu = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
    # #使用pillow打开截图
    # img=Image.open('./main.png')
    # #从截图中按照位置扣除验证码
    # code_img=img.crop(img_tu)
    # # 把扣出来的图,保存到本地
    # code_img.save('./code2.png')

    # 方案二:把图片的base64编码转成图片保存到本地
    img_base64=img_code.get_attribute('src')
    img_base64_real=img_base64.split(',')[-1]
    img_1=base64.b64decode(img_base64_real)
    with open('code.jpg','wb') as f:
        f.write(img_1)


    # 调用超级鹰,完成验证码破解
    # 调用超级鹰识别
    chaojiying = Chaojiying_Client('306334678', 'lqz12345', '903641')  # 用户中心>>软件ID 生成一个替换 96001
    im = open('code.jpg', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    res=chaojiying.PostPic(im, 9004)  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    # 123,155|42,135|11,77---->[[123,155],[42,135],[11,77]]
    print(res)
    result=res['pic_str']
    all_list = []
    if '|' in result:
        list_1 = result.split('|')
        count_1 = len(list_1)
        for i in range(count_1):
            xy_list = []
            x = int(list_1[i].split(',')[0])
            y = int(list_1[i].split(',')[1])
            xy_list.append(x)
            xy_list.append(y)
            all_list.append(xy_list)
    else:
        x = int(result.split(',')[0])
        y = int(result.split(',')[1])
        xy_list = []
        xy_list.append(x)
        xy_list.append(y)
        all_list.append(xy_list)
    print(all_list)


    ### 在页面中点击破解的图案
    #点击  [[123,155],[42,135],[11,77]]
    for item in all_list:
        ActionChains(bro).move_to_element_with_offset(img_code,item[0],item[1]).click().perform()
        time.sleep(1)

    time.sleep(5)
    login_btn.click()
    time.sleep(1)

    # 滑动滑块
    span=bro.find_element_by_id('nc_1_n1z')
    ActionChains(bro).drag_and_drop_by_offset(span, 300, 0).perform()

    time.sleep(30)

    print(bro.get_cookies())
except Exception as e:
    print(e)
finally:
    bro.close()
    bro.quit()  # 关闭整个浏览器
posted @ 2022-05-10 21:06  谢俊杰  阅读(269)  评论(0编辑  收藏  举报