Loading...

百宝箱增加随机笑话功能

日常推荐2个月前更新 Jone
75 0 0

最近想更新下百宝箱小程序功能了,感觉加个随机笑话挺不错的。

苦于别人提供的随机笑话API不稳定(免费的怕跑路),收费的又用不起(哭泣)。反正我的博客基本用不了多少性能(其实是没几个人看,难受),就直接用自己的服务器搭一个呗。

服务器是现成的,之后就需要笑话数据库了。

网上找了个专门笑话的网站,爬了一下午的笑话,目前大约有6000多了。速度贼慢,不敢多线程爬了,搞坏了人都要gg了。

主要代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
import requests
from lxml import etree
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
headers={
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0'
}


def get_page_url(url):
try:
urlhtml = requests.get(url,params=headers)
urlhtml.encoding = "utf-8"
htmlcode = urlhtml.content
html = etree.HTML(htmlcode)
text = html.xpath('//dd/a/@href')
except:
text = 'error'
return text
main_url = "https://www.biedoul.com"
if __name__ == '__main__':
id = 400
while(1):
url = get_page_url(main_url+"/wenzi/"+str(id))
for i in url:
html = requests.get(main_url+i,params=headers)
# print(main_url+i)
html.encoding = 'utf-8'
htmlcode = html.content
html = etree.HTML(htmlcode)
title = html.xpath('//h1/text()')
text = html.xpath('//div[@class="cc2"]/p/text()//div[@class="cc2"]/p/font/text()//div[@class="c"]/div[@class="cc2"]/text()')
f = open("xiaohua/"+title[0]+".txt",'a+')
for t in text:
out = "".join(t.split())
f.write(out)
f.write('\n')
id+=1

感兴趣的可以交流学习,别随便搞别人网站,出了问题与我无瓜。

© 版权声明

相关文章