本帖最后由 looking4freedom 于 2021-9-11 10:28 编辑
二话不说直接上代码,我是用qmsg的api挂在腾讯云进行推送,触发时间的话最好选择下午或晚上,如果想换成其他推送方式的可以稍微改下代码即可
[Python] 纯文本查看 复制代码 import requests
from bs4 import BeautifulSoup
import time
def wool_message():
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}
url = "https://www.youjiangzhijia.com/"
html = requests.get(url=url, headers=headers).text
soup = BeautifulSoup(html, 'lxml')
articles = soup.find_all('article')
content = ""
for article in articles:
article_info = list(article.stripped_strings)
if (article_info[3] == str(time.strftime("%Y-%m-%d", time.localtime())) or article_info[2] == str(
time.strftime("%Y-%m-%d", time.localtime()))) and (
"活动网" in article_info[0] or "活动网" in article_info[1]):
if article_info[0] == '置顶':
content += article_info[2] + '\n'
else:
content += article_info[1] + '\n'
qmsg_url = "https://qmsg.zendee.cn/send/key" # 自己的qmsg的key
data = {
"msg": "【有奖之家今日羊毛信息】:\n" + content,
"qq": " " # 自己的QQ号
}
requests.post(url=qmsg_url, data=data)
def main():
wool_message()
def main_handler(event, context):
return main()
if __name__ == '__main__':
main()
实现效果
总结一下云函数各种报错解决:
1. no module bs4
解决方法:终端 cd 到 src目录执行 pip3 install BeautifulSoup4 -t . 后面有个点哦
2.lxml
用这个命令的话需要单独在src目录下pip install lxml,或者换一个解析器soup = BeautifulSoup(html, 'html.parser')
3.返回为null
说明今天还没更新
如果想要返回的信息有url链接的代码
[Python] 纯文本查看 复制代码 for article in articles:
article_info = list(article.stripped_strings)
if (article_info[3] == str(time.strftime("%Y-%m-%d", time.localtime())) or article_info[2] == str(
time.strftime("%Y-%m-%d", time.localtime()))) and (
"活动网" in article_info[0] or "活动网" in article_info[1]):
res = re.findall(r'.*?href=(.*?) target="_blank', str(article))[0]
if article_info[0] == '置顶':
content += article_info[2] + res + '\n'
else:
content += article_info[1] + res + '\n'
不过不能用qmsg推送,会被诊断为商业行为,其他推送没有试过,有些应该可以 |