admin管理员组文章数量:1530291
准备:
1、intellij idea
2、python
3、selenium
4、phantomJs
1、分析Qzone Html页面
打开手机版qzone https://mobile.qzone.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')
#截图保存到当前项目下
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('//*[@]')
p = self.driver.find_element_by_xpath('//*[@]')
go = self.driver.find_element_by_xpath('//*[@]')
# 移动到账号框模仿键盘输入账号
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
版权声明:本文标题:动态爬虫之手机版QQ空间登录 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1725956496a1050588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论