Python爬取求职网requests库和BeautifulSoup库使用详解

作者:HuiSoul 时间:2021-12-29 09:07:49 

一、requests库

1、requests简介

requests库就是一个发起请求的第三方库,requests允许你发送HTTP/1.1 请求,你不需要手动为 URL 添加查询字串,也不需要对 POST 数据进行表单编码。Keep-alive 和 HTTP 连接池的功能是 100% 自动化的,一切动力都来自于根植在 requests 内部的 urllib3。简单来说有了这个库,我们就能轻而易举向对应的网站发起请求,从而对网页数据进行获取,还可以获取服务器返回的响应内容和状态码。

requesets中文文档页面https://requests.kennethreitz.org/zh_CN/latest/

2、安装requests库

一般电脑安装的Python都会自带这个库,如果没有就可在命令行输入下面这行代码安装


pip install requests

3、使用requests获取网页数据 我们先导入模块


import requests
  • 对想要获取数据的网站发起请求,以下以qq音乐官网为例


res = requests.get('https://y.qq.com/') #发起请求
print(res) #输出<Response [200]>

输出的200其实就是一个响应状态码,下面给大家列出有可能返回的各状态码含义

状态码含义
1xx继续发送信息
2xx请求成功
3xx重定向
4xx客户端错误
5xx服务端错误
  • 获取qq音乐首页的网页源代码


res = requests.get('https://y.qq.com/')  #发起请求
print(res.text)   #res.text就是网页的源代码

4、总结requests的一些方法

属性含义
res.status_codeHTTP的状态码
res.text响应内容的文本
res.content响应内容的二进制形式文本
res.encoding响应内容的编码

既然我们学好了如何获取网页源代码,接下来我们就学习下怎么用BeautifulSoup库对我们获取的内容进行提取。

二、BeautifulSoup库

1、BeautifulSoup简介

BeautifulSoup是Python里的第三方库,处理数据十分实用,有了这个库,我们就可以根据网页源代码里对应的HTML标签对数据进行有目的性的提取。BeautifulSoup库一般与requests库搭配使用。 不熟的HTML标签的最好去百度下,了解一些常用的标签。

2、安装BeautifulSoup库

同样的如果没用这个库,可以通过命令行输入下列代码安装


pip install beautifulsoup4

3、使用BeautifulSoup解析并提取获取的数据


import requests
from bs4 import BeautifulSoup
header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
res = requests.get('https://y.qq.com/',headers=header)  #headers是一种反爬虫措施
soup = BeautifulSoup(res.text,'html.parser')  #第一个参数是HTML文本,第二个参数html.parser是Python内置的编译器
print(soup)  #输出qq音乐首页的源代码

部分输出结果

Python爬取求职网requests库和BeautifulSoup库使用详解

看到输出结果,我们已经成功将网页源代码解析成BeautifulSoup对象。这时可能有人就会问res.text输出的不就是网页代码了吗,何苦再将它转为BeautifulSoup对象呢?
我们先来通过type()函数看下它们的类型


import requests
from bs4 import BeautifulSoup
header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
res = requests.get('https://y.qq.com/',headers=header)  #headers是一种反爬虫措施
soup = BeautifulSoup(res.text,'html.parser')  #第一个参数是HTML文本,第二个参数html.parser是Python内置的编译器
print(type(res.text))
print(type(soup))

输出结果

Python爬取求职网requests库和BeautifulSoup库使用详解

我们可以看到res.text的类型是字符串类型,而soup则是BeautifulSoup对象类型。相比于res.text的字符串类型,soup的BeautifulSoup对象类型拥有着更多可用的方法,以便我们快速提取出需要的数据。这就是为什么我们要多此一步了。

4、BeautifulSoup提取数据的方法

  • 先来了解两个最常用的方法

方法作用
find()返回第一个符合要求的数据
find_all()返回所有符合要求的数据

这两个函数传入的参数就是我们对数据的筛选条件了,我们可以向这两个函数分别传入什么参数呢?

我们以下面在qq音乐首页截取的源代码片段为例,试用两个函数


<div class="index__hd">
           <h2 class="index__tit"><i class="icon_txt">歌单推荐</i></h2>
       </div>
       <!-- 切换 -->
       <div class="mod_index_tab" data-stat="y_new.index.playlist">
<a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"              class="index_tab__item index_tab__item--current js_tag" data-index="0" data-type="recomPlaylist" data-id="1">为你推荐</a>
   <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"              class="index_tab__item js_tag" data-type="playlist" data-id="3056">网络歌曲</a>
              <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"              class="index_tab__item js_tag" data-type="playlist" data-id="3256">综艺</a>
   <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"              class="index_tab__item js_tag" data-type="playlist" data-id="59">经典</a>
   <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"              class="index_tab__item js_tag" data-type="playlist" data-id="3317">官方歌单</a>
   <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"              class="index_tab__item js_tag" data-type="playlist" data-id="71">情歌</a>
       </div>

find()函数

如果想要获取歌单推荐这一行的内容,我们就需要先对歌单推荐的HTML标签进行识别,我们发现它在class="icon_txt"的i标签下,接着就可以通过以下这种方法进行提取


import requests
from bs4 import BeautifulSoup
header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
res = requests.get('https://y.qq.com/',headers=header)  #headers是一种反爬虫措施
soup = BeautifulSoup(res.text,'html.parser')  #第一个参数是HTML文本,第二个参数html.parser是Python内置的编译器
print(soup.find('i', class_='icon_txt'))  #找到 class_='icon_txt'的 i 标签

因为 class 是 Python 中定义类的关键字,所以用 class_ 表示 HTML 中的 class

输出结果

Python爬取求职网requests库和BeautifulSoup库使用详解

find_all()函数

如果我们想要把歌单推荐的全部主题提取下来的话,就要用到find_all()函数

同样的,我们发现这几个主题都在 class="index_tab__item js_tag"的 a标签下,这时为了避免筛选到源代码中其他同为class="index_tab__item js_tag"的标签,我们需要再加多一个条件data-type=“playlist”,具体怎么操作呢?


import requests
from bs4 import BeautifulSoup
header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
res = requests.get('https://y.qq.com/',headers=header)  #headers是一种反爬虫措施
soup = BeautifulSoup(res.text,'html.parser')  #第一个参数是HTML文本,第二个参数html.parser是Python内置的编译器
print(soup.find('i', class_='icon_txt'))
items = soup.find_all('a',attrs={"class" :"index_tab__item js_tag","data-type":"playlist"})

实现的方法就是在第二个参数处传入一个键值对,在里面添加筛选的属性

输出结果

Python爬取求职网requests库和BeautifulSoup库使用详解

通过上面两个小案例,我们发现find()和find_all()函数返回的是Tag对象和Tag对象组成的列表,而我们需要的并不是这一大串东西,我们需要的只是Tag对象的text属性或者href(链接)属性,实现代码如下


import requests
from bs4 import BeautifulSoup
header={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
res = requests.get('https://y.qq.com/',headers=header)  #headers是一种反爬虫措施
soup = BeautifulSoup(res.text,'html.parser')  #第一个参数是HTML文本,第二个参数html.parser是Python内置的编译器
tag1=soup.find('i', class_='icon_txt')
print(tag1.text)
items = soup.find_all('a',attrs={"class" :"index_tab__item js_tag","data-type":"playlist"})
for i in items:  #遍历列表
   tag2=i.text
   print(tag2)

输出结果

Python爬取求职网requests库和BeautifulSoup库使用详解

这样我们就成功把主题的文本内容获取了,而想要提取标签中的属性值,则可以用对象名[‘属性']的方法获取,这里就不演示了

本次分享就到这里了,在下次介绍完反爬虫和如何将数据写进文件的方法后,我会结合我所写的三篇文章的方法来做一个爬取求职网的实例跟大家分享,有兴趣的可以看下,谢谢大家!

希望大家以后多多支持脚本之家!

来源:https://blog.csdn.net/huisoul/article/details/116051442

标签:requests,BeautifulSoup,库
0
投稿

猜你喜欢

  • python3+opencv 使用灰度直方图来判断图片的亮暗操作

    2024-01-01 07:07:23
  • pygame实现简单五子棋游戏

    2022-03-22 08:50:57
  • Python Pandas创建Dataframe数据框的六种方法汇总

    2023-08-25 07:39:16
  • FF下,用 col 隐藏表格列的方法详解!

    2008-04-02 11:35:00
  • Python抓取今日头条街拍图片数据

    2021-11-03 12:30:57
  • 教你轻松学会SQL Server记录轮班的技巧

    2009-02-19 17:38:00
  • 2008年Logo设计10大趋势

    2008-02-28 13:06:00
  • 5个css+div导航菜单

    2011-04-29 12:38:00
  • Python Django框架实现应用添加logging日志操作示例

    2022-09-17 20:59:52
  • PHP ob缓存以及ob函数原理实例解析

    2023-11-18 17:36:14
  • python中SSH远程登录设备的实现方法

    2023-10-24 02:30:58
  • Django app配置多个数据库代码实例

    2023-06-11 09:11:25
  • 表单验证通用脚本(支持所有主流浏览器)

    2010-08-08 08:54:00
  • opencv python 图片读取与显示图片窗口未响应问题的解决

    2021-05-15 22:08:53
  • linux下安装php扩展memcache的方法

    2023-11-21 19:49:02
  • 基于PHP读取csv文件内容的详解

    2023-11-16 04:17:48
  • python使用代理ip访问网站的实例

    2022-02-08 08:16:36
  • python使用scapy模块实现ARP扫描的过程

    2023-07-16 11:55:26
  • 详解用Python处理HTML转义字符的5种方式

    2021-01-27 20:53:17
  • Perl中的正则表达式介绍

    2023-08-11 21:14:25
  • asp之家 网络编程 m.aspxhome.com