今天看了有人用python爬故事,给女票每天发邮箱过去。看了一下感觉很棒,然后又看了评论,有说要是发微信就更好了。
所以。。。。。。
我就做了一个发微信的,哈哈。
先上结果
www.tom61.com/ertongwenxue/shuiqiangushi/index.html 这是故事的链接
用F12可以看见故事的链接在标签dl 下面的 a 里面的 href里面。title是故事名
def getHTMLText(url,headers): #函数访问该网页
try:
r = requests.get(url,headers=headers,timeout = 30)
r.raise_for_status()
r.encoding=r.apparent_encoding
#print(r.text)
return r.text
except:
return "爬取失败"
我们要将里面的链接和名字都存起来,链接可以再次访问,在处理,名字发的时候用上
def parsehtml(namelist,urllist,html): #保存名字和链接
url = "http://www.tom61.com/"
soup = BeautifulSoup(html,'html.parser')
t=soup.find('dl',attrs={'class':'txt_box'})
print(t)
i=t.find_all('a')
print(i)
for link in i:
urllist.append(url+link.get('href'))
namelist.append(link.get('title'))
# print(urllist)
# print(namelist)
这是打印出来的数据
然后我们将处理出来的链接在进行访问,然后处理
def parsehtml2(html): #处理第二次的数据,因为要换行所以我们用到join方法
text=[]
soup=BeautifulSoup(html,'html.parser')
t=soup.find('div',class_='t_news_txt')
for i in t.findAll('p'):
text.append(i.text)
print(text)
return "\n".join(text)
然后就是调用了。调用的时候微信的获取要用到wxpy,这个会出现一张图片,需要用手机微信扫一下进行登录
from wxpy import *
bot = Bot(cache_path=True) #可以保存登录,不用每次都需要登录
还有就是我写了一个文件进行保存每次发的链接是第几个,下次发的时候就+1。
然后创建windows的任务计划程序。目录 我的电脑->右键管理
后面就是起飞了。
最后是源码下载地址:https://download.csdn.net/download/qq_36583051/11042257
里面附带了bat脚本,因为我是用anaconda运行的,要是没有anaconda的,可以直接用pycharm也是可以的。