python 模拟微信浏览器请求_2020-03-06 python selenium 模拟访问网页检查更新并微信通知...

老同学说有个不错的网站,华尔街日报每天更新,但是百度网盘每24小时后就会删除,偶尔忘记去下载就再也找不到了。

简单介绍一下最后成果,在Mac下开发,Ubuntu服务器部署定时检查发送微信到指定用户。在Windows7下也测试通过了。

首先准备环境

这里选择python3,使用 jupyter notebook开发,也可以用重量级的PyCharm或者IDEA,没准备搞那么复杂,所以用了单文件的 notebook 也可以分步运行就好了

pip install selenium jupyter urllib3

开始写代码啦

加载必要的库,准备配置文件

import re

from urllib import request

import configparser

# 生成ConfigParser对象

config = configparser.ConfigParser()

# 读取配置文件

filename = 'config.ini'

config.read(filename, encoding='utf-8')

# 如果没有配置文件,这里可以写入默认的

if not config.has_section('date'):

config.add_section('date')

if not config.has_section('wechat'):

config.add_section('wechat')

config.set('date', 'last_year', '2020')

config.set('date', 'last_month', '3')

config.set('date', 'last_day', '2')

# 登陆 https://sc.ftqq.com/ 获取自己的通知地址

config.set('wechat', 'url', 'https://sc.ftqq.com/replace_you_auth_code.send')

config.write(open(filename, 'w'))

模拟操作 Chrome,打开网站

from selenium import webdriver#导入库

# chromedriver 需要下载

# https://sites.google.com/a/chromium.org/chromedriver/downloads

from selenium.webdriver.chrome.options import Options

# chrome_options = Options()

# #使用无头谷歌浏览器模式 也就是不打开窗口

# chrome_options.add_argument('--headless')

# chrome_options.add_argument('--disable-gpu')

# #这个很重要,一定要把沙箱模式禁止,不然会报错

# chrome_options.add_argument('--no-sandbox')

# Windows上运行需要禁用这个

# chrome_options.add_argument("--disable-dev-shm-usage")

# #指定谷歌浏览器路径

# Windows 指定的是 chrome.exe 要求 chromedriver.exe在path目录,放到 Windows/System32就好了

# chrome_options.binary_location = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'

# linux 也是 Chrome可执行程序,吧chromedriver放到相同目录就行了

# chrome_options.binary_location = '/usr/bin/google-chrome'

# browser = webdriver.Chrome(options=chrome_options)

# Mac系统直接填chromedriver就可以运行了

browser = webdriver.Chrome('/Applications/Google Chrome.app/Contents/MacOS/chromedriver')

开始访问网页了

# 打开需要访问的地址,可以通过 tag/the-wall-street-journal/ 塞选,只有需要访问的内容

url = 'https://yigeplus.top/tag/the-wall-street-journal/'

browser.get(url)#打开浏览器预设网址

# 查找所有 链接地址包含 The Wall Street Journal

elements = browser.find_elements_by_partial_link_text('The Wall Street Journal')

# for n in range(len(elements)):

# v = elements[n]

# print(n, v.text, v.get_attribute('href'))

# 第二个就是(1),最新的

url1 = elements[1].get_attribute('href')

last_year = config.get('date', 'last_year')

last_month = config.get('date', 'last_month')

last_day = config.get('date', 'last_day')

wx_url = config.get('wechat', 'url')

last_date = last_year+'年'+last_month+'月'+last_day+'日'

# 打开子页面

browser.get(url1)#打开浏览器预设网址

# 查找所有 p 文字节点

elements = browser.find_elements_by_tag_name('p')

for n in range(len(elements)):

v = elements[n]

text = v.text

# 如果 text中包含 链接 密码:

if '链接:' in text and '密码:' in text:

title = elements[n-1].text

print(title)

matchObj = re.match( r'.+(\d{4})年(\d+)月(\d+)日.*', title)

linkObj = re.match( r'链接:(.*) 密码:(.{4})', text)

if matchObj and linkObj:

dateStrObj = re.match( r'.+(\d{4}年\d+月\d+日).*', title)

if last_date < dateStrObj.group(1):

print('new date ', dateStrObj.group(1))

# req = request.urlopen(

# wx_url+'?text='+urllib.parse.quote(

# '华尔街日报'+dateStrObj.group(1)+' # 密码'+linkObj.group(2))

# +'&desp='+urllib.parse.quote('['+title+']('+linkObj.group(1)+')'))

# print(req.read())

# config.set('date', 'last_year', matchObj.group(1))

# config.set('date', 'last_month', matchObj.group(2))

# config.set('date', 'last_day', matchObj.group(3))

# config.write(open(filename, 'w'))

print(text)

# print(n, v.text)

输出内容如下,根据需要打开注释,发送微信通知

The Wall Street Journal(华尔街日报)2020年3月3日免费下载:

new date 2020年3月3日

链接:https://pan.baidu.com/s/1GAPe1NIn4dz8pTg3E9WrVQ 密码:x3j6

The Wall Street Journal(华尔街日报)2020年3月2日免费下载:

链接:https://pan.baidu.com/s/10va6BeZgJVtl1GwGFk9h3g 密码:mjvh

The Wall Street Journal(华尔街日报)2020年3月1日免费下载:

链接:https://pan.baidu.com/s/1XNP6jVyeXq1oNkVyVKIn0g 密码:dz6h

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信强制红包是一种红包类型,它是由微信官方推出的一种红包形式,用户在领取红包时需要回答问题或者完成任务,才能领取到红包。如果用户没有回答问题或者完成任务,就无法领取红包。在Python中,可以使用selenium模拟微信网页版的操作,实现自动回答问题或者完成任务,从而领取微信强制红包。 以下是实现微信强制红包的大致步骤: 1. 使用selenium库打开微信网页版,并登录微信账号。 2. 进入红包页面,找到需要领取的强制红包。 3. 判断红包是否需要回答问题或者完成任务,如果需要,就自动回答问题或者完成任务。 4. 领取红包并关闭浏览器。 以下是Python代码示例: ```python from selenium import webdriver import time # 打开微信网页版并登录 driver = webdriver.Chrome() driver.get('https://wx.qq.com/') time.sleep(10) # 等待登录 # 进入红包页面 driver.get('https://wx.qq.com/?&lang=zh_CN#hongbao') time.sleep(5) # 找到需要领取的红包 red_packet = driver.find_element_by_xpath('//div[@class="icon"][@title="微信红包"]') red_packet.click() time.sleep(5) # 判断是否需要回答问题或者完成任务 if driver.find_element_by_xpath('//div[@class="qrcode"]/p'): # 回答问题或者完成任务 answer = driver.find_element_by_xpath('//div[@class="qrcode"]/p').text # 这里可以根据问题类型自动回答问题或者完成任务 # 这里假设问题答案为"123" answer_input = driver.find_element_by_xpath('//input[@class="qrcode_input"]') answer_input.send_keys("123") time.sleep(2) submit_button = driver.find_element_by_xpath('//a[@class="btn"]') submit_button.click() time.sleep(5) # 领取红包并关闭浏览器 open_button = driver.find_element_by_xpath('//a[@class="open"]') open_button.click() time.sleep(5) driver.quit() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值