一、缘由

  这是我之前刚开始学习的时候爬取糗百的练习内容,主要练习的是bs64解析。虽然现在用的不是特别的多,但是当初的时候用起来还是非常的顺手的。

二、代码实现

#coding:utf-8

import requests
from bs4 import BeautifulSoup def download_page(url):
headers={
'User - Agent':"Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 86.0.4240.198 Safari / 537.36"
} #模拟请求登录
r=requests.get(url,headers=headers)
return r.text def get_content(html,page):
output='''第{}页 作者:{}性别:{}年龄:{}点赞:{}\n{}\n----------\n\n'''
soup=BeautifulSoup(html,'html.parser')
con=soup.find('div',id='content')
con_list=con.find_all('div',class_='article')
for i in con_list:
# hot_comments = i.find('div', class_='main-text').get_text() # 获取热评
vote = i.find('i', class_='number').string # 点赞数
author=i.find('h2').string #获得作者的名字
content=i.find('div',class_='content').find('span').get_text() #获取内容
author_info=i.find('div',class_='artciGender') #获取性别,年龄等
if author_info is not None:
class_list=author_info['class']
if 'womenIcon' in class_list:
gender='女'
elif 'manIcon' in class_list:
gender='男'
else:
gender=''
age=author_info.string
else:
gender=''
age=''
save_txt(output.format(page,author,gender,age,vote,content,))#hot_comments def save_txt(*args):
for i in args:
with open('qiubai.txt','a',encoding='utf-8') as f:
f.write(i)
print(i) def main():
for i in range(1,14):
url=f'''https://www.qiushibaike.com/text/page/{i}/'''
html=download_page(url)
get_content(html, i) if __name__ == '__main__':
main()

三、效果展示

base64解析爬取糗百的更多相关文章

  1. Python 爬虫入门(一)——爬取糗百

    爬取糗百内容 GitHub 代码地址https://github.com/injetlee/Python/blob/master/qiubai_crawer.py 微信公众号:[智能制造专栏],欢迎关 ...

  2. Java - XPath解析爬取内容

    code { margin: 0; padding: 0; white-space: pre; border: none; background: transparent; } pre { backg ...

  3. Beautifulsoup网页解析——爬取豆瓣排行榜分类接口

    我们在网页爬取的过程中,会通过requests成功的获取到所需要的信息,而且,在返回的网页信息中,也是通过HTML代码的形式进行展示的.HTML代码都是通过固定的标签组合来实现页面信息的展示,所以,最 ...

  4. BS4爬取糗百

    -- coding: cp936 -- import urllib,urllib2 from bs4 import BeautifulSoup user_agent='Mozilla/5.0 (Win ...

  5. python爬取糗百第一页的笑话

    自学python网络爬虫,发现request比urllib还是要好用一些,因此利用request和BeautifulSoup来实现糗百的首页笑话的抓取.BeautifulSoup通过find和find ...

  6. 爬虫实例之使用requests和Beautifusoup爬取糗百热门用户信息

    这次主要用requests库和Beautifusoup库来实现对糗百的热门帖子的用户信息的收集,由于糗百的反爬虫不是很严格,也不需要先登录才能获取数据,所以较简单. 思路,先请求首页的热门帖子获得用户 ...

  7. python爬取糗百内容

    #-*- coding: utf-8 -*- import urllib import urllib2 import re #页面为1 page=1 url='http://www.qiushibai ...

  8. 接着上次的python爬虫,今天进阶一哈,局部解析爬取网页数据

    *解析网页数据的仓库 用Beatifulsoup基于lxml包lxml包基于html和xml的标记语言的解析包.可以去解析网页的内容,把我们想要的提取出来. 第一步.导入两个包,项目中必须包含beau ...

  9. 全栈爬取-Scrapy框架(CrawlSpider)

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  10. scrapy框架基于CrawlSpider的全站数据爬取

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

随机推荐

  1. Solutions:Elastic SIEM - 适用于家庭和企业的安全防护 ( 三)

  2. Elastic:为Elasticsearch启动https访问

  3. elk使用微信ElartAlert企业微信告警,自定义告警内容

    第一种方式 alert: - "elastalert_modules.wechat_qiye_alert.WeChatAlerter" alert_text: " === ...

  4. 在 Linux 中找出内存消耗最大的进程

    1 使用 ps 命令在 Linux 中查找内存消耗最大的进程 ps 命令用于报告当前进程的快照.ps 命令的意思是"进程状态".这是一个标准的 Linux 应用程序,用于查找有关在 ...

  5. Python对字符数据进行清洗

    import re mystr = "hahaAAA哈哈綂123./!#鱫愛" str1 = ''.join(re.findall('[\u4e00-\u9fa5]',mystr) ...

  6. ArrayList LinkedList Vector之间的区别

    List主要有ArrayList,LinkedList和vector三种实现.这三种都实现了List接口,使用方式也很相似,主要区别在于其实现方式的不同! 这三种数据结构中,ArrayList和Vec ...

  7. python基础作业1

    目录 附加练习题(提示:一步步拆解) 1.想办法打印出jason 2.想办法打印出大宝贝 3.想办法打印出run 4.获取用户输入并打印成下列格式 5 根据用户输入内容打印其权限 6 编写用户登录程序 ...

  8. 15. MongoDB系列之选择片键

    1. 片键类型 1.1 升序片键 升序片键通常类似于date或ObjectId--随着时间稳步增长的字段. 这种模式通常会使MongoDB更难保持块的平衡,因为所有的块都是由一个分片创建的. 1.2 ...

  9. JS复制粘贴效果

    话不多说.直接上代码 HTML: 1 <div> 2 老师入会密码:<input type="text" id="tPass"> < ...

  10. LeetCode------找到所有数组中消失的数字(6)【数组】

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array 1.题目 找到 ...