### 安装依赖库
主要用到requests、bs4、html2text、pytypecho等几个库,使用pip进行安装。
```
pip install requests bs4 html2text pytypecho
```
### 采集文章
以[找IP网](https://zhaoip.xyz)为例,当然此网站文章也是采集而来。
文章列表地址:[https://zhaoip.xyz/category/玩家攻略/](https://zhaoip.xyz/category/%E7%8E%A9%E5%AE%B6%E6%94%BB%E7%95%A5/)
可以看到翻页的地址为列表地址后面加`/页码/`
用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_="样式"))
```
### 写入typecho
```
from pytypecho import Typecho,Post
te = Typecho('https://www.xhuosoft.cn/ 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 删除。