一、缘由

  这是我之前刚开始学习的时候爬取糗百的练习内容,主要练习的是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. 6.使用nexus3配置yum私有仓库

    yum相较于rpm,能够更好地解决安装软件时的依赖包问题,使用yum安装更简单更方便.搭建本地YUM源服务器,可以避免升级安装软件时占用公网带宽:有了本地YUM源服务器,可以解决无法连接Interne ...

  2. 在 CentOS 8/RHEL 8 上安装和使用 Cockpit

    Cockpit 是一个基于 Web 的服务器管理工具,可用于 CentOS 和 RHEL 系统.最近发布的 CentOS 8 和 RHEL 8,其中 cockpit 是默认的服务器管理工具.它的软件包 ...

  3. Linux+Proton without Steam玩火影忍者究极风暴4指南

    首先你需要Proton7.0 without Steam,使用说明和下载链接看这里https://www.cnblogs.com/tubentubentu/p/16716612.html 启动游戏的命 ...

  4. 无需Steam的Proton,在你的Linux运行任意Windows游戏!

    链接: https://pan.baidu.com/s/1QeJxj9_2aZPk2_uZMzpn9A 提取码: v6t6 包含的版本 Proton4.11  Proton4.2  Proton5.0 ...

  5. C# 内存泄漏之 Internal 关键词代表什么?

    一:背景 1. 背景 前段时间有位朋友咨询说他的程序出现了非托管内存泄漏,说里面有很多的 HEAP_BLOCK 都被标记成了 Internal 状态,而且 size 都很大, 让我帮忙看下怎么回事? ...

  6. Servlet(处理乱码,重定向与转发的区别)

    //处理请求乱码 req.setCharacterEncoding("utf-8"); //处理响应乱码 resp.setContentType("text/html;c ...

  7. docker gitlab迁移 备份 部署 搭建以及各种问题

    当前环境 服务器A 服务器B ubuntu docker gitlab(版本一致) docker安装gitlab 由于考虑到gitlab 包含了⾃身的nginx.数据库.端⼝占⽤等等因数,这⾥使⽤的是 ...

  8. commons-fileupload组件和commons-io组件的详细下载

    commons-fileupload组件和commons-io组件的详细下载 1. commons-fileupload组件的下载 下载地址:http://commons.apache.org/fil ...

  9. 二十四、PV与PVC介绍

    PV 与 PVC介绍 一.概念介绍 ​PersistentVolume (PV) 是由管理员设置的存储,它是群集的一部分.就像节点是集群中的资源一样,PV 也是集群中的资源. PV 是Volume 之 ...

  10. Element基本组件

    Element按钮组件: <el-row> <el-button>默认按钮</el-button> <el-button type="primary ...