本帖最后由 youyeaini 于 2019-6-16 01:42 编辑
突然想看看段子,兴起,就写了一段,大家共同学习。
[Python] 纯文本查看 复制代码 # coding:utf-8
import requests
from bs4 import BeautifulSoup
html = 'https://www.qiushibaike.com/hot/'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
res = requests.get(html, params=header).text
bsobj = BeautifulSoup(res, 'lxml')
# 获取用户
bs_title = bsobj.find_all('div', class_='author clearfix')
nick_name_list = []
for nick_name in bs_title:
nick_name_list.append(nick_name.h2.get_text())
# strip掉\n
nick_name_list = [nick.strip() for nick in nick_name_list]
# 获取内容
bs_content = bsobj.find_all('div', class_='content')
content_list = []
for content in bs_content:
content_list.append(content.span.get_text())
content_list = [temp.strip() for temp in content_list]
# 获取好笑
bs_laugh = bsobj.find_all('i', class_='number')
laugh_list = []
for laugh in bs_laugh:
laugh_list.append(laugh.get_text())
laugh_list = [temp.strip() for temp in laugh_list]
# 写入文本
with open('qiubai.txt', 'w+') as f:
# zip强大!
for a, b, c in zip(nick_name_list, content_list, laugh_list):
# 筛选好笑大于1000
if int(c) > 1000:
f.write('用户:' + str(a) + '\n')
f.write('内容:\n' + str(b) + '\n')
f.write('好笑:' + str(c) + '\n')
|