requests基本用法
什么是爬虫
获取网络数据(公开的网络)
爬虫的基本流程
第一步:获取网络数据(requests、selenium)
第二步:解析数据:从获取到的网络数据中提取有效数据(正则、bs4、lxml)
第三步:保存数据(csv、excel、数据等)
requests
python获取网络数据的第三方库(基于http或者https协议的网络请求)
爬虫使用requests的两个场景:直接请求网页地址、对提供网页数据的数据接口发送请求
requests基本用法
对目标网页直接发送请求
response = requests.get(网页地址)
获取指定页面的数据返回一个响应对象
1
2
3
4
5import requests response = requests.get('https://cd.zu.ke.com/zufang') print(response) # <Response [200]> 200 - 请求成功
获取响应的状态码
1
2
3
4print(response.status_code) if response.status_code == 200: pass
获取响应头
1
2print(response.headers)
请求内容(返回的真正有用的数据)
1)response.content - 二进制类型的数据:图片、视频、音频等
例如:图片下载2)response.text - 字符串类型的数据:网页
3)response.json() - 对请求内容做完json解析后的数据:json数据接口
div标签(盒子标签)
将一个范围中涉及到的所有的标签会放到一起。
1
2
3
4
5
6
7
8
9
10div { /* 默认页面的宽度是固定的,高度是无限的。 */ width: 100%; height: 264px; border-top: 5px solid red; border-bottom: 5px solid green; border-left: 5px solid blue; border-right: 5px solid black; }
CSS样式
CSS代码就是写在中的,可以放在head标签或者body标签内。
内部样式表:将style标签放到head标签或者body标签内。
内联样式:可以把样式直接作为一个标签的属性,写入到标签中,使用style属性。
外部样式:有一个后缀名为.css的文件专门用来存放CSS代码,只需要在html代码中使用link标签将CSS文件引入即可。
link标签语法:
如果rel为icon,表示给页面标签页设置图标,type为image/图片格式,表示引入xxx格式的图片
rel=“icon” type=“image/图片格式(png、jpg)” href=“图片的链接和路径”
rel为stylesheet,表示给页面引入样式表,type固定为text/css
rel=“stylesheet” type=“text/css” href=“css文件的链接或者路径”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <!-- 内部样式 --> <style> /* 这是CSS注释 */ h1 { color: purple; } </style> <!-- 外部样式 --> <link rel="stylesheet" type="text/css" href="css/样式.css" /> </head> <!-- 内联样式 --> <body style="background-color: antiquewhite;"> <h1>这里是h1标签</h1> </body> </html>
CSS选择器
- 通配符选择器。:* :能改变所有标签的样式。
- 标签选择器。 标签名 :不加限定条件,能够修改页面中所有某标签。
- id选择器。#设置的标签名{}
- class选择器。.设置的标签名
- 父子选择器。父标签 > 子标签:最终选择修改的是子标签。
- 后代选择器。祖先标签 后代标签:最终选择修改的是后代标签。
- 兄弟选择器。 长兄标签 ~ 弟弟标签。
- 相邻兄弟选择器。 刘关张:刘 + 关 关 + 张。
- nth-child选择器。 a:nth-child(数字) ->找div标签下第二个a标签(html下标从1开始)(数字=3:找第几个标签,如果发现一致,就选择,如果不一致,就都不选择)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #one{} </style> </head> <body id="one"> <div> <a href=""></a> </div> </body> </html>
CSS文字常用属性:
color:修改文字颜色
font-size:字体尺寸
font-family:字体
text-align:位置(left、right、center)
text-decoration:在文字的下方或者上方或中间添加一条横线
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> /* 所有选择器可以组合使用 */ /* 标签选择器 */ /* 将页面中所有的div改为某样式 */ /* div { border: 1px dotted black; width: 100px; height: 100px; } */ /* 将body标签的子标签中第一个div标签改为某样式 */ body>div:nth-child(1) { border-top: 1px dotted black; border-bottom: 1px solid red; border-left: 1px dashed blue; border-right: 5px double green; width: 100px; height: 100px; /*表示修改四个角 */ border-radius: 100% 50%; } #one { border-top: 5px solid yellowgreen; width: 100px; height: 200px; background-color: antiquewhite; } .one { border: 3px double blue; border-radius: 100%; } p { /* color: red; */ /* color: #ff0000; */ /* color: rgb(255, 0, 0); */ color: rgba(255, 0, 0, 0.5); text-align: center; font-size: 50px; /* 下划线 */ /* text-decoration: underline; */ /* 上划线 */ /* text-decoration: overline; */ /* 从文字中间穿过 */ text-decoration: line-through; } </style> </head> <body> <div></div> <div id="one"></div> <div class="one"> <div id="one"></div> </div> <p><b>今日安排</b></p> <ul> <li>上课</li> <li>上课</li> <li>上课</li> <li>上课</li> </ul> </body> </html>
最后
以上就是鲜艳月饼最近收集整理的关于python爬虫和前端(部分)的全部内容,更多相关python爬虫和前端(部分)内容请搜索靠谱客的其他文章。
发表评论 取消回复