|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
一直想找一个可以自动更新文字的网站,写到别的程序,最近找到一个网站感觉文字很不错,就爬取下了了.
(没有什么技术含量,打包也很简单,用pyinstaller跑一次就可以了,所以就不放成品了)
先上代码:
- import requests
- import json
- import time
- def get_beautiful():
- res = requests.get('http://v1.hitokoto.cn') # 目标地址
- data = json.loads(res.text)
- beautiful = data['hitokoto']
- with open('一言.txt', 'a', encoding='utf-8') as file: # 这里我使用了a方法,因为每一次循环会追加进去,没有用w
- print(beautiful)
- file.write(f'{beautiful}\n')
- number = input('请输入获取的次数:') # 没有写异常处理
- num = 0
- for _ in range(int(number)):
- num += 1
- get_beautiful()
- if num % 20 == 0:
- time.sleep(5) # 毕竟是免费接口,让程序没爬取20次就休息5秒钟
- else:
- print('感谢使用,再见!')
- time.sleep(2)
复制代码
爬取到的是json个是,from_who字段有时候是None,就只是爬取了正文部分
json
|
|