爬糗百(一)
本帖最后由 youyeaini 于 2019-6-16 01:42 编辑突然想看看段子,兴起,就写了一段,大家共同学习。
# 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 =
# 获取内容
bs_content = bsobj.find_all('div', class_='content')
content_list = []
for content in bs_content:
content_list.append(content.span.get_text())
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 =
# 写入文本
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')
自己占一个沙发,请教大家,为什么不能设置代码风格了。。。 虽然不太懂——但是感觉很棒——谢谢分享! 天王盖地虎 也在学python爬虫,就是基础差了点 有没有手机版去广告的? 88491354@qq.com 发表于 2019-6-16 08:44
天王盖地虎
R U 250?:keai
页:
[1]