老同学说有个不错的网站,华尔街日报每天更新,但是百度网盘每24小时后就会删除,偶尔忘记去下载就再也找不到了。
我也推荐一下,点开有积分 https://yigeplus.top/?mref=sloanyyc
简单介绍一下最后成果,在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
网友评论