爬虫学习总结-程序员宅基地

技术标签: 爬虫  python  前端  macos  

记录mac使用chromedriver的解决办法

CSDN解决办法链接chr=webdriver.Chrome(r'/Users/a./opt/chromedriver/chromedriver') # 带完整路径的写法

我的插件所在地址/Users/a./opt/chromedriver/chromedriver
解决办法:
直接在新开窗口时候:r‘插件所在位置’

记录mac在终端使用jupyter notebook

把要使用的目录在终端打开
输入:jupyter notebook
![image.png](https://img-blog.csdnimg.cn/img_convert/08a366db9fe4bb9d63c6c54d36325167.png#clientId=u5490f16f-dd42-4&from=paste&height=864&id=udd95ebb3&margin=[object Object]&name=image.png&originHeight=1728&originWidth=1746&originalType=binary&ratio=1&size=788996&status=done&style=stroke&taskId=u152289f8-4212-4e12-a3be-105c4a2b8aa&width=873)

一、基础知识

1、为什么要爬

爬取网络数据:如果需要的数据市场上没有,或者不愿意购买,那么可以选择爬虫方式,自己动手丰衣足食
爬虫Crawler,蜘蛛Spider,机器人Robot从搜索引擎机器人程序发展而来。虽然两者在功能上很相似,但是爬虫程序却可以通过分析遍历来的网页中含有的网页链接信息,自动获取下一步需要遍历的网页,这个过程可以自动的持续进行下去;且对网页内容进行解析。
![image.png](https://img-blog.csdnimg.cn/img_convert/a70c1ea4e8908cbd6db411defde22e47.png#clientId=u0f2313b8-0a51-4&from=paste&height=720&id=ub0eb5adf&margin=[object Object]&name=image.png&originHeight=1440&originWidth=1795&originalType=binary&ratio=1&size=2961298&status=done&style=none&taskId=uc4c96571-5841-4a65-a359-3676f7b7dce&width=897.5)

互联网虽然很复杂,但其实就是一张大图——可以把每一个网页当作一个节点,把那些超链接当作链接网页的弧。
网络爬虫从任何一个网页出发,用图的遍历算法,自动地访问到每一个网页,并对其进行进行解析。

2、爬虫的作用

数据分析----社交网络、情感分析、购物助手、排行榜数据等
自动下载----图片、文档、音乐等自动操作----抢票软件、自动关注

3、爬虫如何工作

网络爬虫模拟浏览器发送网络请求,接收请求响应,按照一定的规则,自动地抓取互联网信息的程序。遵循标准的HTTP协议利用超链接和Web文档检索方法遍历万维网
爬虫就是模拟浏览器的行为,越像越好,越像就越不容易被发现。

模仿浏览器的操作去收集数据

4、法律与道德问题
网络蜘蛛需要抓取网页,不同于一般的访问,如果控制不好,则会引起网站服务器负担过重。比如,淘宝就因为雅虎搜索引擎的爬虫抓取数据引起淘宝网服务器的不稳定。
好的网络爬虫,需要遵守robots协议(Robots Exclusion Protocol),即网络爬虫排除标准。
robots协议以文件的形式(robots.txt)存放于网站根目录下。它告诉爬虫网站中的哪些内容是不应被获取的,哪些是可以获取的。

robots.txt

robot.txt 中声明了哪些文件是可以获取的,哪些是不能获取的
如百度的:https://www.baidu.com/robots.txt

![image.png](https://img-blog.csdnimg.cn/img_convert/0c4ffafdfe9efa3ac0d338ec2f59b85e.png#clientId=u0f2313b8-0a51-4&from=paste&height=494&id=u4415d01f&margin=[object Object]&name=image.png&originHeight=987&originWidth=1436&originalType=binary&ratio=1&size=267542&status=done&style=none&taskId=u0fd7dd6e-de80-4b67-94bb-7bddbfb174b&width=718)

爬虫与反爬虫

![image.png](https://img-blog.csdnimg.cn/img_convert/8c1b70dfd784cfb9790cb2a227237f56.png#clientId=u0f2313b8-0a51-4&from=paste&height=720&id=u89a7adda&margin=[object Object]&name=image.png&originHeight=1440&originWidth=1767&originalType=binary&ratio=1&size=1954429&status=done&style=none&taskId=u9240cf3a-2bf3-47c9-bc98-16c1fec7086&width=883.5)
最基本的:user-agent

二、网页基本知识

1、认识HTTP协议

HTTP(超文本传输协议)是应用层上的一种客户机/服务器的通信协议,是Web的核心,由请求和响应构成。 协议:协议规定了通信双方必须遵守的数据传输格式,这样通信双方按照约定的格式才能准确的通信。
![image.png](https://img-blog.csdnimg.cn/img_convert/1b6393803f0e19cb2cd2f50e1d573b93.png#clientId=u0f2313b8-0a51-4&from=paste&height=141&id=u1e8ca6cf&margin=[object Object]&name=image.png&originHeight=160&originWidth=575&originalType=binary&ratio=1&size=13444&status=done&style=stroke&taskId=u8d4b566c-e95a-4f1f-827a-518d99aab80&width=508.5)
![image.png](https://img-blog.csdnimg.cn/img_convert/d13fc59b8bae3fad975a124d9b099977.png#clientId=u0f2313b8-0a51-4&from=paste&height=276&id=u76404966&margin=[object Object]&name=image.png&originHeight=418&originWidth=776&originalType=binary&ratio=1&size=130033&status=done&style=stroke&taskId=ud68e9620-a0e4-4b1f-85ce-adaab6e664a&width=513)

URL地址格式

HTTP协议基于URL访问服务器资源
URL(Uniform Resource Locator) :统一资源定位符
格式说明:URL由三部分组成
第一部分:协议,如http, https, ftp等
第二部分:主机地址及端口号(缺省端口为80)
第三部分:资源的具体地址,如目录和文件名等
![image.png](https://img-blog.csdnimg.cn/img_convert/c6c1608c85c477c319b9c918599bcc33.png#clientId=u0f2313b8-0a51-4&from=paste&height=357&id=u2204b1a5&margin=[object Object]&name=image.png&originHeight=1440&originWidth=1724&originalType=binary&ratio=1&size=935041&status=done&style=none&taskId=uf41799f8-8e9e-4c55-9972-0fa8a81c130&width=427)

如:
https://pandas.pydata.org/docs/reference/index.html

![image.png](https://img-blog.csdnimg.cn/img_convert/42342fa30d64ab4032e8e19b575a4747.png#clientId=u0f2313b8-0a51-4&from=paste&height=280&id=uf0e0bffa&margin=[object Object]&name=image.png&originHeight=405&originWidth=771&originalType=binary&ratio=1&size=71975&status=done&style=stroke&taskId=uc91ce928-ed38-40a5-ab5c-d7f5c71874b&width=533.5)

user-Agent 请求头

如下在开发者工具中(F12)
network中反应的第一个,最下面
如,我的请求头 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36

![image.png](https://img-blog.csdnimg.cn/img_convert/9ced0b92b858deaf22b068a681f31552.png#clientId=u0f2313b8-0a51-4&from=paste&height=465&id=u4aeb6834&margin=[object Object]&name=image.png&originHeight=930&originWidth=872&originalType=binary&ratio=1&size=228032&status=done&style=none&taskId=u970f9207-0fbe-4033-b03e-5e339a96916&width=436)
在爬虫和反爬虫的对弈中,User-Agent首当其冲
服务器端首先会根据User-Agent判断对方是哪种浏览器
依据浏览器发展的历程,服务器会针对不同浏览器发送其能接受的格式的HTML
Python程序发送的请求中User-Agent默认的取值是“Python……”,相当于明晃晃的告诉服务器“我是个爬虫!”,
因此爬虫第一步:必须对User-Agent进行伪装

在python爬虫中,首先要伪装User-Agent

HTTP响应 状态码

![image.png](https://img-blog.csdnimg.cn/img_convert/9a2cbb09cf3d7fba56c30405e4c05630.png#clientId=u0f2313b8-0a51-4&from=paste&height=126&id=u9743da80&margin=[object Object]&name=image.png&originHeight=106&originWidth=362&originalType=binary&ratio=1&size=11707&status=done&style=stroke&taskId=ubd41978f-a9ec-4ee5-bf8a-09a989b921c&width=432)
![image.png](https://img-blog.csdnimg.cn/img_convert/967934ae47831ee8235389e15acb573f.png#clientId=u0f2313b8-0a51-4&from=paste&height=297&id=u49bc2b34&margin=[object Object]&name=image.png&originHeight=280&originWidth=405&originalType=binary&ratio=1&size=31514&status=done&style=stroke&taskId=u466cc55b-3f4f-49b7-adb1-8217e12e1bd&width=429.5)

常用200,表示请求成功

2xx:表示服务器成功接收请求并已完成整个处理过程,常用200表示请求成功。
3xx:表示未完成请求,浏览器需进一步细化请求。例如:请求的资源位于另一个地址,需进行重定向,响应代码为302。
4xx:表示浏览器端的请求有错误,常用404表示服务器无法找到被请求的页面、403表示服务器拒绝浏览器的访问(权限不够)、405表示服务器不支持浏览器的请求方式等。
5xx:表示服务器端出现错误,常用500表示请求未完成,在服务器遇到不可预知的情况,属于服务器端程序错误 5xx是服务器端出问题,一般不常见。

2、网页组成(三大件)

  • HTML
  • CSS
  • JS

HTML+CSS+JavaScript

复杂的网页主要由3部分组成
结构 (Structure)
表现(Presentation)
行为(Behavior)

HTML-结构:决定网页的结构和内容元素标记头部文件,元素标记网页名称,元素标记网页主体,<table>元素标记表格等<br/> CSS-表现(样式):设定网页的表现样式描述各类元素的视觉特征<br/> JavaScript:行为,控制网页的行为按钮动作等,通过</table></body>

静态网页和动态网页

静态网页:随着HTML代码的生成,页面的内容和显示效果就基本上不会发生变化了——除非你修改页面代码。
动态网页:是指跟静态网页相对的一种网页编程技术。动态网页的页面代码虽然没有变,但是显示的内容却是可以随着时间、用户请求数据的变化而发生改变的。

爬虫取到的页面仅仅是一个静态的页面,即网页的源代码,就像在浏览器上的“查看网页源代码”一样。一些动态的东西如JavaScript脚本执行后所产生的信息,是抓取不到的。

3、浏览器工具(F12)

推荐Chrome的F12

使用浏览器开发者工具查看网页请求
查找请求URL
查找请求head
使用浏览器开发者工具查看网页结构(这个很重要,爬到数据很容易,关键是把数据分类保存下来)

![image.png](https://img-blog.csdnimg.cn/img_convert/352e21915f39cb5af4088cf51daa8746.png#clientId=u0f2313b8-0a51-4&from=paste&height=720&id=ufea07fda&margin=[object Object]&name=image.png&originHeight=1440&originWidth=2525&originalType=binary&ratio=1&size=1028484&status=done&style=stroke&taskId=u8077235e-ecb9-4d16-8df7-f82f8bd17fb&width=1262.5)

查看页面元素

![image.png](https://img-blog.csdnimg.cn/img_convert/96cac12df78dfc97c21dabc107d6cb4b.png#clientId=u0f2313b8-0a51-4&from=paste&height=720&id=uaaaecb49&margin=[object Object]&name=image.png&originHeight=1440&originWidth=2173&originalType=binary&ratio=1&size=814735&status=done&style=none&taskId=u36b40808-6ded-4774-9f8b-a2edce7c96f&width=1086.5)

常用的标签

常用属性:
class:特征标识
href:超链接地址(爬取目标)
src:图片下载地址(爬取目标)

三、使用request库编写基本爬虫

  • 使用requests库请求数据
  • 为请求添加参数
  • 伪装请求报头

Python 提供了很多库来支持 HTTP 协议的网络编程,目前普遍使用requests库
requests 实现了 HTTP 协议中绝大部分功能,它提供的功能包括 Keep-Alive、连接池、Cookie持久化、内容自动解压、HTTP代理、SSL认证、连接超时、Session等很多特性,并同时兼容 Python2 和 Python3。
requests库中文文档 https://requests.readthedocs.io/zh_CN/latest/index.html

实例_从豆瓣爬取图片

  1. 构造URL
  2. 请求数据
  3. 解析响应数据
  4. 保存数据到本地

演示:

![image.png](https://img-blog.csdnimg.cn/img_convert/1059bb38dd182d2d5f3b8b421cd9893e.png#clientId=u0f2313b8-0a51-4&from=paste&height=522&id=u1173263c&margin=[object Object]&name=image.png&originHeight=1044&originWidth=943&originalType=binary&ratio=1&size=121185&status=done&style=stroke&taskId=u29061dd2-2351-4dc3-af35-a087e219b7e&width=471.5)![image.png](https://img-blog.csdnimg.cn/img_convert/5b618593a5f7f3cef0d008262c8d3b7e.png#clientId=u0f2313b8-0a51-4&from=paste&height=272&id=udbf79c06&margin=[object Object]&name=image.png&originHeight=543&originWidth=709&originalType=binary&ratio=1&size=387968&status=done&style=stroke&taskId=u41495638-4d40-4047-b900-6d75af13b73&width=354.5)

1、获取URL

即获取所要内容的地址

在网页要查看的元素上,单击鼠标右键,选择“检查”
在Elements中,选中要复制的URL,进行复制

2、发出请求

使用requests.get(URL地址)发出请求

3、解析请求

请求返回 Response 对象,Response 对象是对HTTP 协议中服务端返回给浏览器的响应数据的封装

响应对象中的属性
状态码status_code——200 表示正常
编码方式 encoding(从HTTP的header中的charset字段中提取)
编码方式 apparent_encoding(从网页的内容中分析网页的编码方式,比encoding更加准确)
响应首部:headers(字典类型)
响应体:content、text

响应体
content、text、json

HTTP返回的响应消息中很重要的一部分内容是响应体,与响应体相关的属性有:content、text、json。
content 为 byte 类型,适合直接将内容保存到文件系统或者传输到网络中(声音、图像、视频、PDF文档等二进制数据)
text 是 string 类型,如一个普通的 HTML 页面,需要对文本进一步分析时,使用 text如果使用第
三方开放平台或者API接口爬取数据时,返回的内容是JSON格式的数据,那么可以直接使用 响应对象.json() 返回一个经过处理后的字典对象。

响应头

![image.png](https://img-blog.csdnimg.cn/img_convert/6f0637f6e218ed53b780d886a0277ce1.png#clientId=u0f2313b8-0a51-4&from=paste&height=460&id=u2236ba57&margin=[object Object]&name=image.png&originHeight=1440&originWidth=1839&originalType=binary&ratio=1&size=592169&status=done&style=stroke&taskId=ua53d1c8b-bbaf-40c5-818f-1cb72378094&width=588)
![image.png](https://img-blog.csdnimg.cn/img_convert/e9236e299c7b7b7810969f112f917fee.png#clientId=u0f2313b8-0a51-4&from=paste&height=449&id=ub971573b&margin=[object Object]&name=image.png&originHeight=384&originWidth=460&originalType=binary&ratio=1&size=42805&status=done&style=stroke&taskId=ubdde32af-ba2c-4627-9ea2-743452e7599&width=538)
print("响应的状态码:{}".format(res.status_code))
print("该响应的数据类型为:{}".format(res.headers['content-type']))
print("该响应的数据量为{}字节".format(res.headers['content-length']))

4、保存数据保存数据

with open("tmp/图片.jpg", "wb") as f:
f.write(res.content) #res.content为二进制类型的响应数据
w表示写,b表示二进制文件

注意保存到指定路径下的时候,文件要存在,否则会下图

![image.png](https://img-blog.csdnimg.cn/img_convert/e2eed41d15617bd38db82a31e7ccc668.png#clientId=u0f2313b8-0a51-4&from=paste&height=187&id=ub22daf7e&margin=[object Object]&name=image.png&originHeight=250&originWidth=645&originalType=binary&ratio=1&size=43389&status=done&style=none&taskId=ud7cc57be-2442-4fd9-88d1-a4d1cc65d69&width=482.5)

解决中文乱码

将响应的apparent_encoding 编码送给响应的encoding属性:
res.encoding = res.apparent_encoding !!!
![image.png](https://img-blog.csdnimg.cn/img_convert/f0c2ac160d3273a368aa8bdb600fd36a.png#clientId=u0f2313b8-0a51-4&from=paste&height=263&id=u34beda41&margin=[object Object]&name=image.png&originHeight=259&originWidth=449&originalType=binary&ratio=1&size=44977&status=done&style=stroke&taskId=ua7d16e52-e5ed-4dbd-8de3-bafeabbf1f8&width=455.5)

2、为请求添加参数

请求参数:向请求提交的待处理数据
构建请求查询参数
get请求:将参数附加在URL请求地址中(请求行)
post请求:将参数附加在请求实体中

get请求

将参数附加在URL请求地址中(请求行)

很多URL都带有很长一串参数,称这些参数为URL的查询参数,用”?”附加在URL链接后面,多个参数之间用”&”隔开

以百度搜索为例,如何进行带参数请求?
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=python&fenlei=256&rsv_pq=8876fa4d0015d0ce&rsv_t=4cd8T9fffLAqYmzGhd5p%2FF6ntO1iZckFwgxj3Fa8bhMt6Bmg6

方法1
构造URL

# 采用构造URL方式访问
res = requests.get('http://baidu.com?wd=python')  #url?key=value

# 打印输出该 URL,能看到URL已被正确编码
print(res.url)

    'http://baidu.com/?wd=python'

方法2
将参数放在params参数中传递,适用于参数较多的场景

# 采用构造URL方式访问
response = requests.get("https://baidu.com", 
                                      params={
    'wd': 'python'})

# 打印输出该 URL,能看到URL已被正确编码
print(res.url)

post方式

将参数附加在请求实体中

requests 可以非常灵活地构建post请求需要的数据,如果服务器要求发送的数据是表单数据,则可以指定关键字参数 data;
如果要求传递 JSON 格式字符串参数,则可以使用json关键字参数,参数的值都可以字典的形式传递。

方式1
作为表单数据传输给服务器

# 作为表单数据传输给服务器

# URL: www.httpbin.org, 这是一个简单的 HTTP 请求和响应服务
payload = {
    'key1': 'value1', 'key2': 'value2'}
res = requests.post("http://www.httpbin.org/post", data=payload)
print(res.text)

![image.png](https://img-blog.csdnimg.cn/img_convert/5ee803958d2d96f6c47159280fdc3c49.png#clientId=u5490f16f-dd42-4&from=paste&height=323&id=u9817082c&margin=[object Object]&name=image.png&originHeight=374&originWidth=732&originalType=binary&ratio=1&size=76313&status=done&style=stroke&taskId=ub88e3f45-82ff-4f16-9e92-2c4664edbb2&width=632)

方式2
作为 json字符串格式传输给服务器
JSON格式的优势,数据可以嵌套

# 作为 json 格式的字符串格式传输给服务器
import json

payload = {
    'key1': 'value1', 'key2': 'value2'}
res = requests.post("http://www.httpbin.org/post", json=payload)
# 查看相应内容,注意看Content-Type的变化
print(res.text)

![image.png](https://img-blog.csdnimg.cn/img_convert/7e20d2f91bb15a52198479f88d583be5.png#clientId=u5490f16f-dd42-4&from=paste&height=367&id=u98241ee0&margin=[object Object]&name=image.png&originHeight=358&originWidth=591&originalType=binary&ratio=1&size=74528&status=done&style=stroke&taskId=u266565f6-7361-41a0-8af7-aa3d2a4e67f&width=606.5)

3、伪装请求头 user-agent

requests 可以很简单地指定请求首部字段Headers,直接传递一个字典对象给参数 headers 即可。

user-agent(用户代理):HTTP协议中的一部分,属于请求头,作用是描述发出HTTP请求的终端的一些信息。
网站常用这个参数来分辨爬虫,对于不是正常浏览器的用户进行屏蔽。

修改user-agent:储存系统和浏览器的型号版本,通过修改它来伪装为浏览器,以此来蒙蔽服务器

# 请求一个返回Json格式数据的API
hd = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0'}
res = requests.get('https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8', headers = hd)
# 查看文件类型
print(res.headers['Content-Type'])  #application/json;
# 以JSON格式查看内容
print(res.json())

四、使用BeautifulSoup库解析网页

requests 库请求的数据结果
JSON 格式,数据对开发者来说最友好
HTML 文档,最为常见
XML 格式

BeautifulSoup :一个用于解析 HTML 文档的 Python 库,通过 BeautifulSoup,只需要用很少的代码就可以提取出 HTML 中任何感兴趣的内容,此外,它还有一定的 HTML 容错能力,对于一个格式不完整的HTML 文档,它仍可以正确处理。

BeautifulSoup 用来解析网页

1、使用前准备工作

pip install bs4

官方文档:http://beautifulsoup.readthedocs.io/zh_CN/v4.4.0

HTML 结构

![image.png](https://img-blog.csdnimg.cn/img_convert/84d3623c6005327b343f08f41b286aac.png#clientId=u5490f16f-dd42-4&from=paste&height=199&id=u7139e018&margin=[object Object]&name=image.png&originHeight=398&originWidth=580&originalType=binary&ratio=1&size=30371&status=done&style=stroke&taskId=u652afb15-30a5-46ba-a448-5855c53758b&width=290)
它由很多标签(Tag)组成,比如 html、head、title等等都是标签
一个标签对构成一个节点,比如 …是一个根节点
节点之间存在某种关系,比如 h1 和 p 互为邻居,他们是相邻的兄弟(sibling)节点
h1 是 body 的直接子(children)节点,还是 html 的子孙(descendants)节点
body 是 p 的父(parent)节点,html 是 p 的祖辈(parents)节点
嵌套在标签之间的字符串是该节点下的一个特殊子节点,比如 “hello, world” 也是一个节点,只不过没名字。

2、如何使用BeautifulSoup

构建一个 BeautifulSoup 对象需要两个参数
第一个参数:要解析的 HTML 文本字符串
第二个参数:告诉 BeautifulSoup 使用哪个解析器来解析 HTML
![image.png](https://img-blog.csdnimg.cn/img_convert/4dc6853d0388ae038507ff257bde45f8.png#clientId=u5490f16f-dd42-4&from=paste&height=259&id=u576c9efe&margin=[object Object]&name=image.png&originHeight=518&originWidth=1114&originalType=binary&ratio=1&size=108695&status=done&style=stroke&taskId=u4c56673a-90f5-4169-8a23-33ba8c98115&width=557)

BeautifulSoup 负责操作数据(增删改查)
BeautifulSoup优点:API人性化,支持CSS选择器
BeautifulSoup缺点:基于HTML DOM树,会载入整个HTML文档,解析整个DOM树,因此时间和内存开销都会很大
BeautifulSoup用来解析 HTML较为简单的页面

3、标签选择器

  • 获取标签 soup.标签名
  • 获取标签间文字:soup.标签名.string
  • 获取属性字典:soup.标签名.attrs
  • 获取属性取值:soup.标签名[属性名]

![image.png](https://img-blog.csdnimg.cn/img_convert/c82c32fe9562f5abe481881cb02bdfd2.png#clientId=u5490f16f-dd42-4&from=paste&height=146&id=ua43dea13&margin=[object Object]&name=image.png&originHeight=179&originWidth=756&originalType=binary&ratio=1&size=24591&status=done&style=stroke&taskId=u52d9597d-1468-4c19-8276-9d467f690d4&width=615)
BeatifulSoup 主要的数据类型
beautifulSoup:代表整个 HTML 文档
tag:每个标签节点
navigableString:包裹在Tag对象中的字符串

print(type(soup))  # bs4.BeautifulSoup
print(soup) # 无格式
print(soup.prettify())  #缩进对齐格式

获取标签

![image.png](https://img-blog.csdnimg.cn/img_convert/b7952c300612d064b4707c08558baa5a.png#clientId=u5490f16f-dd42-4&from=paste&height=289&id=u68a401f8&margin=[object Object]&name=image.png&originHeight=349&originWidth=725&originalType=binary&ratio=1&size=42498&status=done&style=stroke&taskId=u5a830f7c-8e17-42ca-9924-1ea1dfee6d4&width=600.5)

获取标签间的文字:

提取标签间文字的方法
.string:返回当前节点中的内容,但是当前节点包含多个子节点时,.string不知道要获取哪一个节点中的内容,故返回空
.text:返回当前节点所包含的所有文本内容,包括当前节点的子孙节点
![image.png](https://img-blog.csdnimg.cn/img_convert/12af9c8f0025e5b77b7a2a06064128ba.png#clientId=u5490f16f-dd42-4&from=paste&height=278&id=ubf9be84b&margin=[object Object]&name=image.png&originHeight=335&originWidth=751&originalType=binary&ratio=1&size=50776&status=done&style=stroke&taskId=u407d21b3-7d80-458c-8ac8-c7d973dec99&width=622.5)

获取属性字典

![image.png](https://img-blog.csdnimg.cn/img_convert/6f8fb6fbfed1df958d307a21577e7eee.png#clientId=u5490f16f-dd42-4&from=paste&height=83&id=u635a7e1f&margin=[object Object]&name=image.png&originHeight=89&originWidth=623&originalType=binary&ratio=1&size=16300&status=done&style=stroke&taskId=u8d6288dc-7859-4467-b26e-1f875cb18eb&width=577.5)

获取属性取值

![image.png](https://img-blog.csdnimg.cn/img_convert/157aa86f08b58d559407e18f2cde8060.png#clientId=u5490f16f-dd42-4&from=paste&height=73&id=ue983d2fb&margin=[object Object]&name=image.png&originHeight=74&originWidth=604&originalType=binary&ratio=1&size=16100&status=done&style=stroke&taskId=uab663124-781e-4101-8b3d-0b4503c0d27&width=592)

使用标签选择器实例

![image.png](https://img-blog.csdnimg.cn/img_convert/2460b557a8aba50354a93382b4a6af50.png#clientId=u5490f16f-dd42-4&from=paste&height=501&id=ud420039e&margin=[object Object]&name=image.png&originHeight=593&originWidth=771&originalType=binary&ratio=1&size=79058&status=done&style=stroke&taskId=u5e15a740-1f9a-43d0-89d0-9d8e6142405&width=651.5)

  • .string 返回当前节点内的内容,假如当前节点包含多个自节点时候,不知道获取哪一节点内的内容,故返回值为空
  • .text 返回当前节点内包含的所有文本内容
    • 包括当前节点的子节点

4、遍历DOM树

可以对元素的子元素、父元素、兄弟元素等进行导航,从而遍历整个网页
contents属性:直接子节点列表(文本、节点)
descendants属性:子孙节点
parent属性:父节点
parents属性:祖先节点
next_sibling属性:下一个兄弟节点
previous_sibling属性:上一个兄弟节点

![image.png](https://img-blog.csdnimg.cn/img_convert/37ee23b54cbf4b6d86105406ad6f0e89.png#clientId=u5490f16f-dd42-4&from=paste&height=276&id=u192705ba&margin=[object Object]&name=image.png&originHeight=330&originWidth=725&originalType=binary&ratio=1&size=41461&status=done&style=stroke&taskId=uaa0c9d0e-5406-4516-a856-b110f32e3c3&width=605.5)

下行遍历

![image.png](https://img-blog.csdnimg.cn/img_convert/5a2ecd12e5e74cf46e9d3f3889d6ff2d.png#clientId=u5490f16f-dd42-4&from=paste&height=125&id=u35905851&margin=[object Object]&name=image.png&originHeight=143&originWidth=726&originalType=binary&ratio=1&size=28033&status=done&style=stroke&taskId=u4d605cd4-27bd-471d-8291-7960f47b8c9&width=635)
![image.png](https://img-blog.csdnimg.cn/img_convert/08dc364dd04488fa38329f3d77cc184b.png#clientId=u5490f16f-dd42-4&from=paste&height=273&id=ud88d9f30&margin=[object Object]&name=image.png&originHeight=314&originWidth=737&originalType=binary&ratio=1&size=66407&status=done&style=stroke&taskId=u1b3d3b47-ea7f-4f7c-95a0-446dd18bde4&width=640.5)
![image.png](https://img-blog.csdnimg.cn/img_convert/95007555508d2a5ca0a2dba6d4dec767.png#clientId=u5490f16f-dd42-4&from=paste&height=271&id=u16d7046a&margin=[object Object]&name=image.png&originHeight=314&originWidth=740&originalType=binary&ratio=1&size=52419&status=done&style=stroke&taskId=u6c993c25-7584-4126-bbb1-01a98760372&width=639)

上行遍历

![image.png](https://img-blog.csdnimg.cn/img_convert/cc0e8e4c51d4e53afae1d94d8cad42fd.png#clientId=u5490f16f-dd42-4&from=paste&height=106&id=u4a255569&margin=[object Object]&name=image.png&originHeight=107&originWidth=637&originalType=binary&ratio=1&size=14445&status=done&style=stroke&taskId=u4c73c1bf-7d3b-4670-96d4-77092edead8&width=629.5)

平行遍历

![image.png](https://img-blog.csdnimg.cn/img_convert/7326fc0b32ed11b30a3ba1864ca80d76.png#clientId=u5490f16f-dd42-4&from=paste&height=149&id=u66aee146&margin=[object Object]&name=image.png&originHeight=176&originWidth=767&originalType=binary&ratio=1&size=38067&status=done&style=stroke&taskId=u1b324519-a46b-48ff-98a4-7b08717bb2b&width=647.5)

搜索文档树

搜索文档树是通过指定标签名来搜索元素,还可以通过指定标签的属性值来精确定位某个节点元素
find_all()
find()
这两个方法在 BeatifulSoup 和 Tag 对象上都可以被调用

![image.png](https://img-blog.csdnimg.cn/img_convert/a90199076109745cb5e592cc1a17391b.png#clientId=u5490f16f-dd42-4&from=paste&height=263&id=u642a045d&margin=[object Object]&name=image.png&originHeight=269&originWidth=661&originalType=binary&ratio=1&size=43687&status=done&style=stroke&taskId=ufc7d3923-bc0d-45b8-b31e-7c731ab245f&width=646.5)
![image.png](https://img-blog.csdnimg.cn/img_convert/60103818ed8f5e1b460c543f90456fa0.png#clientId=u5490f16f-dd42-4&from=paste&height=285&id=ud6a93223&margin=[object Object]&name=image.png&originHeight=331&originWidth=759&originalType=binary&ratio=1&size=63579&status=done&style=stroke&taskId=u8120147b-d013-4fd8-be38-d87c23581db&width=653.5)

5、BeautifulSoup库网络爬取实战

学术会议论文集爬取

![image.png](https://img-blog.csdnimg.cn/img_convert/9616b11111f2e23d30eccd64c74d2b13.png#clientId=ub4f2245c-ef0e-4&from=paste&height=588&id=ucea5c8b9&margin=[object Object]&name=image.png&originHeight=855&originWidth=813&originalType=binary&ratio=1&size=338654&status=done&style=none&taskId=ue8e8fc5c-59da-435d-aab9-e7a32766ff0&width=559.5)

6、CSS选择器

比单纯的标签选择器更具体
标签名.classname

![image.png](https://img-blog.csdnimg.cn/img_convert/a4caeaeaf6146bb24765b0a7b6c884d0.png#clientId=ub4f2245c-ef0e-4&from=paste&height=180&id=u23732c2e&margin=[object Object]&name=image.png&originHeight=198&originWidth=595&originalType=binary&ratio=1&size=22034&status=done&style=stroke&taskId=u6e3a4733-93e9-4597-8933-51cc6a01fdf&width=541.5)
![image.png](https://img-blog.csdnimg.cn/img_convert/4dcc89b61368a39fb01973ac60d27f3b.png#clientId=ub4f2245c-ef0e-4&from=paste&height=207&id=u45c93716&margin=[object Object]&name=image.png&originHeight=292&originWidth=770&originalType=binary&ratio=1&size=35457&status=done&style=stroke&taskId=ufd792785-7728-479c-84d7-d068c1d18d5&width=547)
![image.png](https://img-blog.csdnimg.cn/img_convert/bb32a55dbcc371bb6d9e679c9fd5f048.png#clientId=ub4f2245c-ef0e-4&from=paste&height=228&id=u53adc191&margin=[object Object]&name=image.png&originHeight=275&originWidth=702&originalType=binary&ratio=1&size=31583&status=done&style=stroke&taskId=ue8ae01cc-fb6e-4343-923b-6f9c9920cf0&width=581)

7、实战_利用BeautifulSoup爬取猫眼

换页的信息隐藏在URL中,因此可以通过修改URL的参数来实现爬取

url = '[http://maoyan.com/board/4?offset='](http://maoyan.com/board/4?offset=') + str(i*10)

五、自动化爬取动态网站的方法

  • selenium
  • 安装ChromeDriver,即Chrome浏览器的驱动
    • ![image.png](https://img-blog.csdnimg.cn/img_convert/21b878f0ce5398e684b4a23bb46e08d8.png#clientId=u5490f16f-dd42-4&from=paste&height=231&id=uf67be872&margin=[object Object]&name=image.png&originHeight=462&originWidth=910&originalType=binary&ratio=1&size=106383&status=done&style=stroke&taskId=udbc59e59-0bcd-4f8e-817d-c4fef55a051&width=455)

测试selenium自动执行

应对JavaScript等解决不了的反爬虫技术
跳转被隐藏在JavaScript代码中没有URL,即不能修改页面参数来实现换行

中国大学2020排名

六、反爬虫

![image.png](https://img-blog.csdnimg.cn/img_convert/5827603506322abc1a6e4552166983ae.png#clientId=u5490f16f-dd42-4&from=paste&height=159&id=u2d6a82c8&margin=[object Object]&name=image.png&originHeight=167&originWidth=474&originalType=binary&ratio=1&size=19090&status=done&style=none&taskId=udc176952-10b6-403a-bbed-70539ee66f7&width=452)

最后,请利用所学的实践一波吧

![image.png](https://img-blog.csdnimg.cn/img_convert/1d40a90d419fd0e62e21e346fb394607.png#clientId=u5490f16f-dd42-4&from=paste&height=658&id=u9a5c4a8c&margin=[object Object]&name=image.png&originHeight=1316&originWidth=2560&originalType=binary&ratio=1&size=986922&status=done&style=stroke&taskId=u03e825ff-a089-4bbb-8c73-bb7ff044320&width=1280)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_51502400/article/details/122147769

智能推荐

从零开始搭建Hadoop_创建一个hadoop项目-程序员宅基地

文章浏览阅读331次。第一部分:准备工作1 安装虚拟机2 安装centos73 安装JDK以上三步是准备工作,至此已经完成一台已安装JDK的主机第二部分:准备3台虚拟机以下所有工作最好都在root权限下操作1 克隆上面已经有一台虚拟机了,现在对master进行克隆,克隆出另外2台子机;1.1 进行克隆21.2 下一步1.3 下一步1.4 下一步1.5 根据子机需要,命名和安装路径1.6 ..._创建一个hadoop项目

心脏滴血漏洞HeartBleed CVE-2014-0160深入代码层面的分析_heartbleed代码分析-程序员宅基地

文章浏览阅读1.7k次。心脏滴血漏洞HeartBleed CVE-2014-0160 是由heartbeat功能引入的,本文从深入码层面的分析该漏洞产生的原因_heartbleed代码分析

java读取ofd文档内容_ofd电子文档内容分析工具(分析文档、签章和证书)-程序员宅基地

文章浏览阅读1.4k次。前言ofd是国家文档标准,其对标的文档格式是pdf。ofd文档是容器格式文件,ofd其实就是压缩包。将ofd文件后缀改为.zip,解压后可看到文件包含的内容。ofd文件分析工具下载:点我下载。ofd文件解压后,可以看到如下内容: 对于xml文件,可以用文本工具查看。但是对于印章文件(Seal.esl)、签名文件(SignedValue.dat)就无法查看其内容了。本人开发一款ofd内容查看器,..._signedvalue.dat

基于FPGA的数据采集系统(一)_基于fpga的信息采集-程序员宅基地

文章浏览阅读1.8w次,点赞29次,收藏313次。整体系统设计本设计主要是对ADC和DAC的使用,主要实现功能流程为:首先通过串口向FPGA发送控制信号,控制DAC芯片tlv5618进行DA装换,转换的数据存在ROM中,转换开始时读取ROM中数据进行读取转换。其次用按键控制adc128s052进行模数转换100次,模数转换数据存储到FIFO中,再从FIFO中读取数据通过串口输出显示在pc上。其整体系统框图如下:图1:FPGA数据采集系统框图从图中可以看出,该系统主要包括9个模块:串口接收模块、按键消抖模块、按键控制模块、ROM模块、D.._基于fpga的信息采集

微服务 spring cloud zuul com.netflix.zuul.exception.ZuulException GENERAL-程序员宅基地

文章浏览阅读2.5w次。1.背景错误信息:-- [http-nio-9904-exec-5] o.s.c.n.z.filters.post.SendErrorFilter : Error during filteringcom.netflix.zuul.exception.ZuulException: Forwarding error at org.springframework.cloud..._com.netflix.zuul.exception.zuulexception

邻接矩阵-建立图-程序员宅基地

文章浏览阅读358次。1.介绍图的相关概念  图是由顶点的有穷非空集和一个描述顶点之间关系-边(或者弧)的集合组成。通常,图中的数据元素被称为顶点,顶点间的关系用边表示,图通常用字母G表示,图的顶点通常用字母V表示,所以图可以定义为:  G=(V,E)其中,V(G)是图中顶点的有穷非空集合,E(G)是V(G)中顶点的边的有穷集合1.1 无向图:图中任意两个顶点构成的边是没有方向的1.2 有向图:图中..._给定一个邻接矩阵未必能够造出一个图

随便推点

MDT2012部署系列之11 WDS安装与配置-程序员宅基地

文章浏览阅读321次。(十二)、WDS服务器安装通过前面的测试我们会发现,每次安装的时候需要加域光盘映像,这是一个比较麻烦的事情,试想一个上万个的公司,你天天带着一个光盘与光驱去给别人装系统,这将是一个多么痛苦的事情啊,有什么方法可以解决这个问题了?答案是肯定的,下面我们就来简单说一下。WDS服务器,它是Windows自带的一个免费的基于系统本身角色的一个功能,它主要提供一种简单、安全的通过网络快速、远程将Window..._doc server2012上通过wds+mdt无人值守部署win11系统.doc

python--xlrd/xlwt/xlutils_xlutils模块可以读xlsx吗-程序员宅基地

文章浏览阅读219次。python–xlrd/xlwt/xlutilsxlrd只能读取,不能改,支持 xlsx和xls 格式xlwt只能改,不能读xlwt只能保存为.xls格式xlutils能将xlrd.Book转为xlwt.Workbook,从而得以在现有xls的基础上修改数据,并创建一个新的xls,实现修改xlrd打开文件import xlrdexcel=xlrd.open_workbook('E:/test.xlsx') 返回值为xlrd.book.Book对象,不能修改获取sheett_xlutils模块可以读xlsx吗

关于新版本selenium定位元素报错:‘WebDriver‘ object has no attribute ‘find_element_by_id‘等问题_unresolved attribute reference 'find_element_by_id-程序员宅基地

文章浏览阅读8.2w次,点赞267次,收藏656次。运行Selenium出现'WebDriver' object has no attribute 'find_element_by_id'或AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'等定位元素代码错误,是因为selenium更新到了新的版本,以前的一些语法经过改动。..............._unresolved attribute reference 'find_element_by_id' for class 'webdriver

DOM对象转换成jQuery对象转换与子页面获取父页面DOM对象-程序员宅基地

文章浏览阅读198次。一:模态窗口//父页面JSwindow.showModalDialog(ifrmehref, window, 'dialogWidth:550px;dialogHeight:150px;help:no;resizable:no;status:no');//子页面获取父页面DOM对象//window.showModalDialog的DOM对象var v=parentWin..._jquery获取父window下的dom对象

什么是算法?-程序员宅基地

文章浏览阅读1.7w次,点赞15次,收藏129次。算法(algorithm)是解决一系列问题的清晰指令,也就是,能对一定规范的输入,在有限的时间内获得所要求的输出。 简单来说,算法就是解决一个问题的具体方法和步骤。算法是程序的灵 魂。二、算法的特征1.可行性 算法中执行的任何计算步骤都可以分解为基本可执行的操作步,即每个计算步都可以在有限时间里完成(也称之为有效性) 算法的每一步都要有确切的意义,不能有二义性。例如“增加x的值”,并没有说增加多少,计算机就无法执行明确的运算。 _算法

【网络安全】网络安全的标准和规范_网络安全标准规范-程序员宅基地

文章浏览阅读1.5k次,点赞18次,收藏26次。网络安全的标准和规范是网络安全领域的重要组成部分。它们为网络安全提供了技术依据,规定了网络安全的技术要求和操作方式,帮助我们构建安全的网络环境。下面,我们将详细介绍一些主要的网络安全标准和规范,以及它们在实际操作中的应用。_网络安全标准规范