当前位置:首页 » python爬虫 » 正文

基于Python爬取51cto博客页面信息过程解析

看: 1074次  时间:2020-10-11  分类 : python爬虫

介绍

提到爬虫,互联网的朋友应该都不陌生,现在使用Python爬取网站数据是非常常见的手段,好多朋友都是爬取豆瓣信息为案例,我不想重复,就使用了爬取51cto博客网站信息为案例,这里以我的博客页面为教程,编写的Python代码!

实验环境

1.安装Python 3.7

2.安装requests, bs4模块

实验步骤

1.安装Python3.7环境

2.安装requests,bs4 模块

打开cmd,输入:pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple/

再安装bs4, 输入:pip install bs4 -i https://pypi.tuna.tsinghua.edu.cn/simple/

安装后面加上-i https://pypi.tuna.tsinghua.edu.cn/simple/ 表示从清华镜像下载模块,速度会提高好多!

3.编写代码

# 爬取51cto博客页面
import requests
import bs4
import re

def open_url(url):
  # 使用代理
  # proxies = {"http": "127.0.0.1:1080", "https": "127.0.0.1:1080"}
  headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
           'Chrome/57.0.2987.98 Safari/537.36'}

  # res = requests.get(url, headers=headers, proxies=proxies)
  res = requests.get(url, headers=headers)

  return res

def find_titles(res):
  soup = bs4.BeautifulSoup(res.text, 'html.parser')

  # 博客名
  titles = []
  targets = soup.find_all("a", class_="tit")
  for each in targets:
    titles.append(each.text.strip())

  # 阅读量
  reads = []
  read1 = soup.find_all("p", class_="read fl on")
  read2 = soup.find_all("p", class_="read fl")
  for each in read1:
    reads.append(each.text)
  for each in read2:
    reads.append(each.text)

  # 评论数
  comment = []
  targets = soup.find_all("p", class_='comment fl')
  for each in targets:
    comment.append(each.text)

  # 收藏
  collects = []
  targets = soup.find_all("p", class_='collect fl')
  for each in targets:
    collects.append(each.text)

  # 汇总
  result = []
  length = len(titles)
  for i in range(length):
    result.append(titles[i] + '\n' + reads[i] + ' ' + comment[i] + ' ' + collects[i] + '\n')

  return result

# 找出一共有多少个页面
def find_depth(res):
  soup = bs4.BeautifulSoup(res.text, 'html.parser')
  depth = soup.find('li', class_='next').previous_sibling.previous_sibling.text

  return int(depth)

def main():
  host = "https://blog.51cto.com/13760351"
  res = open_url(host)
  depth = find_depth(res)

  result = []
  for i in range(1, depth + 1):  #范围是1到6页
    url = host + '/p' + str(i)  #网页链接是后面加上/pi
    res = open_url(url)
    result.extend(find_titles(res))

 # 写入文本文件中
  with open("51cto博客.txt", "w", encoding="utf-8") as f:
    for each in result:
      f.write(each)

#主程序入口
if __name__ == "__main__":
  main()

4.运行代码,查看效果

阿里云服务器安装postfix--邮箱服务(排坑过程详解) 荐 置顶
阅读 10000+ 评论 2 收藏 9
(教科书式教程!)在VMware Workstation 14 上安装CentOS 7 【送安装包】 置顶
阅读 10000+ 评论 10 收藏 0
CentOS 7 系统YUM 安装MySQL 5.7
阅读 136 评论 0 收藏 1
Docker 安装(官方安装方法)
阅读 153 评论 0 收藏 0
运维实用工具推荐
阅读 159 评论 0 收藏 0
K8S 部署 Rancher 2.X 版本
阅读 1068 评论 0 收藏 0
python 练习题(三)
阅读 260 评论 0 收藏 0
DNS--域名解析
阅读 170 评论 0 收藏 0
python 练习题(二)
阅读 353 评论 0 收藏 0
K8S 使用Dashboard部署nginx群集
阅读 124 评论 0 收藏 0
CentOS 7 离线部署K8S群集
阅读 280 评论 0 收藏 0
MySQL函数--数学函数
阅读 136 评论 0 收藏 0
CentOS 7 安装SVN
阅读 192 评论 2 收藏 0
MYSQL 数据库索引
阅读 178 评论 0 收藏 0
CentOS 8 深入优化 nginx 服务
阅读 509 评论 0 收藏 0
CentOS 7 搭建nginx网页服务
阅读 207 评论 0 收藏 0
Linux 文本处理利器--Awk常用命令
阅读 940 评论 0 收藏 0
Liunx 文件的查找
阅读 163 评论 0 收藏 0
Linux 流编辑--Sed命令及语法
阅读 178 评论 0 收藏 0
MYSQL行为
阅读 110 评论 0 收藏 0
MYSQL数据库--连接查询
阅读 269 评论 0 收藏 0
MYSQL 数据库基本操作
阅读 271 评论 0 收藏 0
Ansible 常用模块
阅读 119 评论 0 收藏 0
CentOS 8部署自动化运维工具-- Ansible
阅读 358 评论 0 收藏 0
CentOS 7 部署百万pv项目(高可用架构)
阅读 2139 评论 0 收藏 4
MYSQL查询--子查询,合并查询
阅读 242 评论 0 收藏 0
MySQL伪事务和性能
阅读 178 评论 0 收藏 0
MYSQL数据库--定义表和字段的别名
阅读 256 评论 0 收藏 0
MYSQL查询--聚合函数查询
阅读 132 评论 0 收藏 0
MYSQL 查询命令--单表查询
阅读 205 评论 0 收藏 0
ELK 平台收集Tomcat日志记录
阅读 316 评论 0 收藏 0
CentOS 8 部署ELK日志分析平台
阅读 730 评论 0 收藏 0
Docker 数据管理和网络通信
阅读 246 评论 0 收藏 0
Docker 镜像创建方法
阅读 143 评论 0 收藏 0
Docker 容器操作
阅读 195 评论 0 收藏 0
Docker 镜像操作
阅读 143 评论 0 收藏 0
CentOS 8 系统图形化安装教程(超详细)
阅读 4570 评论 0 收藏 0
Docker 部署 nginx + tomcat
阅读 501 评论 0 收藏 0
Docker 部署war包项目
阅读 2173 评论 0 收藏 1
华为云服务器安装 Docker 容器
阅读 901 评论 0 收藏 0
CentOS 8 安装docker 容器
阅读 4848 评论 0 收藏 0
解决 confluence 和 jira 数据包问题
阅读 225 评论 0 收藏 0
CentOS 7.6 搭建Gitlab教程
阅读 234 评论 0 收藏 0
confluence 配置邮件服务器
阅读 516 评论 0 收藏 0
Shell脚本实现 tomcat 日志定时切割
阅读 263 评论 0 收藏 0
阿里云服务器Centos7 配置465端口号发送邮件
阅读 774 评论 0 收藏 0
MQTT 限制匿名用户访问,开启用户密码认证
阅读 640 评论 0 收藏 0
CentOS 7.6 部署Redis 数据库
阅读 150 评论 0 收藏 0
CentOS 7.6 破解安装 Confluence 和 Jira(送安装包和破解工具)
阅读 5800 评论 0 收藏 0
Centos 7 安装MQTT(EMQ)服务端
阅读 943 评论 0 收藏 0
CentOS 7 实现yum安装最新php版本
阅读 2527 评论 0 收藏 0
Python3 对文件操作
阅读 886 评论 0 收藏 0
Python3 操作Mysql数据库
阅读 820 评论 0 收藏 1
Selenium 定位元素的8种方法介绍
阅读 1536 评论 0 收藏 0
Python Selenium 自动配置zabbix邮箱报警功能
阅读 568 评论 0 收藏 0
Zabbix 配置QQ邮箱报警通知
阅读 2043 评论 0 收藏 0
CentOS 7 系统搭建私服仓库Nexus
阅读 664 评论 0 收藏 1
解决CentOS 7 最小化安装后无ifconfig命令
阅读 687 评论 0 收藏 0
CentOS 7 安装禅道并绑定公司内网邮箱
阅读 1161 评论 0 收藏 0
Shell 脚本自动安装公司内部邮箱服务器--Postfix
阅读 7066 评论 0 收藏 4
解决虚拟机在桥接模式下设置静态ip,无法上外网的各种问题
阅读 10000+ 评论 6 收藏 0
Shell 脚本自动安装cobbler(改进+填坑)
阅读 10000+ 评论 3 收藏 0
Shell 脚本自动安装 Cobbler (知识点+踩坑点)
阅读 5292 评论 0 收藏 6
CentOS 7.4 系统升级至7.6
阅读 4738 评论 4 收藏 0
超实用技能--快速查看windows WiFi密码
阅读 4518 评论 0 收藏 0
Jenkins自动化部署安卓项目
阅读 7574 评论 0 收藏 1
记一次阿里云服务器安装Python的血泪史
阅读 1671 评论 6 收藏 1
Jenkins自动化部署nodejs项目(前端项目)
阅读 1445 评论 0 收藏 2
Jenkins自动部署发布Java代码(完整教程)
阅读 9192 评论 0 收藏 1
Jenkins自动化打包--war包
阅读 9444 评论 2 收藏 3
shell 搭配 python 自动安装zabbix监控端详解
阅读 1112 评论 0 收藏 1
在CentOS 7系统下升级 Jenkins版本(含排坑过程)
阅读 7223 评论 4 收藏 0
Window 10 安装python 3.7 + selenium (附最新安装包)
阅读 2984 评论 0 收藏 0
CentOS 7 轻松安装Jenkins
阅读 3259 评论 0 收藏 1
脚本安装Discuz论坛(shell + Python 实现自动化安装)
阅读 1783 评论 0 收藏 0
Shell脚本自动源码包安装LA/NMP架构详解(赠软件包+脚本)
阅读 3068 评论 0 收藏 0
shell脚本一键安装Tomcat服务
阅读 2192 评论 0 赞赏 1
【值得收藏!】Vi 文本编辑器细讲
阅读 789 评论 4 收藏 1
企业应用---部署LAMP平台 + Discuz论坛【赠安装包】
阅读 2032 评论 0 收藏 2
【高效装机】PXE实现无人值守安装CentOS 6
阅读 4144 评论 0 收藏 1
在CentOS 7 上安装Oracle 12c 【赠安装包】
阅读 4776 评论 0 收藏 1
轻松部署MFS分布式文件系统 【内附源码包】
阅读 3049 评论 0 收藏 1
Memcached + LAMP 架构安装详解【送源码包】
阅读 3523 评论 4 收藏 0
部署MySQL-MMM 高可用群集
阅读 1123 评论 0 收藏 0
搭建 MySQL 读写分离(内附源码包)
阅读 573 评论 2 收藏 1
Mysql数据库主从复制
阅读 716 评论 0 收藏 0
mysql数据库基本命令---多条数据的同时操作
阅读 999 评论 0 收藏 0
使用Haproxy 搭建web群集
阅读 496 评论 2 收藏 0
MySQL数据库源码包安装(5.7最新版本)
阅读 568 评论 0 收藏 0
解决PXE装机报警----重复安装系统,无法进入用户登录界面
阅读 1521 评论 0 收藏 0
解决PXE装系统报警问题----写入空间不足
阅读 705 评论 0 收藏 0
Nginx 服务语法正确,服务启动失败解决方法
阅读 767 评论 0 收藏 0
CentOS 7 实现Nginx+Tomcat 负载均衡
阅读 685 评论 0 收藏 0
Nginx 构建虚拟主机
阅读 618 评论 0 收藏 0
Nginx 服务安全优化---隐藏版本号、网页缓存、日志分割
阅读 599 评论 0 收藏 0
轻松部署Tomcat服务器
阅读 346 评论 0 收藏 0
Apache网页安全优化----设置防盗链和隐藏版本信息
阅读 748 评论 0 收藏 0
手工编译安装Apache网站(赠送源码包)
阅读 638 评论 0 收藏 1
Apache构建虚拟web主机
阅读 747 评论 0 收藏 0
CentOS 6 yum 安装Apache网站
阅读 2072 评论 0 收藏 0
搭建AWStats日志分析系统(内附awstats软件包)
阅读 10000+ 评论 0 收藏 0
图文详解cobbler 自动装机教程 (内附epel源)
阅读 10000+ 评论 8 收藏 2
DNS分离解析教学 超简单!
阅读 679 评论 0 收藏 0
FTP服务最安全认证模式---虚拟账户访问
阅读 4440 评论 0 收藏 0
FTP服务器--实现本地用户访问,拒绝匿名用户
阅读 749 评论 0 收藏 0
快速搭建FTP服务器--实现匿名用户上传和下载
阅读 1296 评论 2 收藏 0
Linux 6.5 服务器上搭建本地yum仓库
阅读 2206 评论 0 收藏 0
【超好用】弱口令扫描工具John the Ripper 图文教程(附软件包)
阅读 2669 评论 4 收藏 1
详述在Linux上挂载window共享文件夹
阅读 634 评论 12 收藏 0
Redhat Linux 6.5 图文详细安装教程【附安装包】
阅读 7550 评论 8 收藏 2

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。

标签:requests  selenium  nginx  

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!