python自动生成鸡汤文_使用Python抓取鸡汤和天气,使用script自动运行每日推送至微信好友,python,爬取,心灵鸡汤,并用,脚本,每天,给...

需要微信的itchat库,若没有的老铁,先:

pip install itchat

若速度太慢,先在

点击这里,找到itchat库

下载后,再cmd:

pip install C:\a\b\itchat.whl #所在文件的绝对路径

安装完成后,代码,如下,话不多说,改个你所在的城市和微信好友,直接运行即可。

import json

import requests

import itchat

import urllib.request

from bs4 import BeautifulSoup

def jitang():

url='http://open.iciba.com/dsapi/'

r=requests.get(url)

all=json.loads(r.text)

chinese=all['content']

english=all['note']

soup=english+'\n'+chinese

return soup

def get_weather(city_pinyin):

header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64;\

rv:23.0) Gecko/20100101 Firefox/23.0'}

website = "http://www.tianqi.com/" + city_pinyin + ".html"

req = urllib.request.Request(url=website, headers=header)

page = urllib.request.urlopen(req)

html = page.read()

soup = BeautifulSoup(html.decode("utf-8"), "html.parser")

nodes = soup.find_all('dd')

tody_weather = ""

for node in nodes:

temp = node.get_text()

if (temp.find('[切换城市]')):

temp = temp[:temp.find('[切换城市]')]

tody_weather += temp

tianqi = "".join([s for s in tody_weather.splitlines(True)

if s.strip()])

return tianqi

if __name__ == '__main__':

itchat.auto_login(hotReload=True)

result = '早上好,morning,老铁'+'\n'+jitang() + '\n' + get_weather('beijing')#你所在的城市

#print(result)

name_list = ["微信好友"] #你的微信好友可以输入多个

for name in name_list:

temp_info = itchat.search_friends(name)[0]["UserName"]

itchat.send(result, toUserName=temp_info)

itchat.run()

运行结果如下图:


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部