动态爬虫之手机版QQ空间登录

准备:

1、intellij idea
2、python
3、selenium
4、phantomJs

1、分析Qzone Html页面

打开手机版qzone https://mobile.qzone.qq.com

动态爬虫之手机版QQ空间登录

按照上面流程复制账号、密码和登录按钮的的XPath粘贴到记事本中

2、超链

1、构建浏览器并且设置请求头
2、开始请求
3、模仿用户输入
4、输入验证码
5、自动登录
6、完整代码

3、编写爬虫代码

首先创建一个浏览器对象和设置请求头

# 导入驱动包
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
class qzone_dlewares(object):
    # 浏览器请求头
    headers = {'Accept': '*/*',
               'Accept-Language': 'en-US,en;q=0.8',
               'Cache-Control': 'max-age=0',
               'User-Agent': 'Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255',
               'Connection': 'keep-alive', }
    
    #初始化浏览器
    def __init__(self,userName='' ,password = '', *args, **kwargs):
        self.userName = userName
        self.password = password
        desired_capabilities = DesiredCapabilities.PHANTOMJS.copy()
        for key, value in self.headers.items():
            desired_capabilities['phantomjs.page.customHeaders.{}'.format(key)] = value
        self.driver = webdriver.PhantomJS(desired_capabilities=desired_capabilities)
        # 设置屏幕大小
        self.driver.set_window_size(414, 736)

开始请求 截图

def startQzoneRequest(self):
    #开始请求qzone
    self.driver.get('https://mobile.qzone.qq.com')
    #截图保存到当前项目下
    self.driver.save_screenshot('screenshot.png')

截图成功会在 project 下面生成screenshot.png

模仿用户输入 登录关键性代码

import time
from selenium.webdriver import ActionChains
    def loginQzone(self):
        u = self.driver.find_element_by_xpath('//*[@id="u"]')
        p = self.driver.find_element_by_xpath('//*[@id="p"]')
        go = self.driver.find_element_by_xpath('//*[@id="go"]')
        
        # 移动到账号框模仿键盘输入账号
        action = ActionChains(self.driver)
        action.move_to_element(u)
        action.click(u)
        # 模仿键盘输入账号
        action.send_keys(self.userName)
        
        # 移动到密码输入框
        action.move_to_element(p)
        action.click(p)
        # 模仿键盘输入密码
        action.send_keys(self.password)
        
        # 点击登录
        action.move_by_offset(go.location['x'], go.location['y'])
        action.click(go)
        
        # 执行登录
        action.perform()
        # 休息1秒保证能执行
        time.sleep(1)
        # 截图保存到当前项目下
        self.driver.save_sc
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值