主要用到requests、bs4、html2text、pytypecho等几个库,使用pip进行安装。
pip install requests bs4 html2text pytypecho以找IP网为例,当然此网站文章也是采集而来。
文章列表地址:https://zhaoip.xyz/category/玩家攻略/
可以看到翻页的地址为列表地址后面加/页码/
用requests配合BeautifulSoup非常简单就能拿到文章地址。
for i in range(1,14):
res = requests.get('https://zhaoip.xyz/category/'+str(i)).text
bs = BeautifulSoup(res,'lxml')
links = bs.find('div',class_="区域样式").findAll('a')拿到文章地址之后就是拿文章的标题和内容,使用bs也是很轻松就能拿到,将内容使用html2text改成markdown格式。
res = requests.get(link).text
bs = BeautifulSoup(res,'lxml')
title = bs.h2.text
content = html2text.html2text(str(bs.find('div',class_="样式"))需要IP可访问 薪火IP https://www.xhuosoft.cn/
from pytypecho import Typecho,Post
te = Typecho('typecho博客的xmlrpc地址', username='后台用户名', password='密码')
post = Post(title=title, categories = ['文章分类'],description=content)
te.new_post(post, publish=True)
time.sleep(5)#休息5秒以上就是采集文章到typecho的相关流程。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。