day3.15-requests和bs4的用法
css基本语法以及css选择器
css基本语法
<!--
css(层叠样式表,简称样式,样式表)负责网页内容的布局和样式
1.css代码写在哪
1)内联样式表:将css代码写在标签的style属性中
2)内部样式表:将css代码卸载style标签中
3)外部样式表:将css代码写在css文件中,然后再html代码中用link标签导入
2.css代码怎么写
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 1.内联样式表 -->
<p style="color: red;">内联样式表</p>
<a href="https://www.baidu.com/" style="color: red;font-size: 30px;">百度</a>
<!-- 2.内部样式表 -->
<br><br>
<p id="p1">内部样式表</p>
<a id="a1" href="https://www.iqiyi.com/">爱奇艺</a>
<style type="text/css">
color: lawngreen;
}
color:
font-size: 25px;
}
</style>
<!-- 3.外部样式表 -->
<br><br>
<link rel="stylesheet" type="text/css" href="css/test.css"/>
<a id="a2" href="https://v.qq.com/">腾讯视频</a>
</body>
</html>
css选择器
<!--
1.元素选择器(标签选择器)
将标签名作为选择器,选中所有的指定标签:
例如:a{} - 选中所有的a标签
p{} - 选择所有的p标签
2.id选择器
在id属性值前加
例如:
3.class选择器
在class属性值前加.作为一个选择器,选中class属性值是指定值的标签
不同的标签可以有相同的class值,同一个标签可以有不同的class值
例如:.c1 - 选中所有class值为c1的标签
.c1.c2 - 选中所有class值同时为c1和c2的标签
4.群组选择器
将多个选择器用逗号隔开并作为一个选择器,选中每个独立选择器选中的所有标签
例如:p,a{} - 选中所有的p标签和a标签
p,.c1{} - 选中所有的p标签和class值1为c1的标签
5.子代选择器
将多个选择器用>符号隔开作为一个选择器
例如:div>
6.后代选择器
将多个选择器用空格隔开作为一个选择器
例如:div div a - 选中div下面的div标签的a标签 (前后是后代选择器)
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 1.元素选择器 -->
<!-- <style type="text/css">
p{
color:
background-color: blue;
}
a{
color:
background-color:
}
</style> -->
<!-- 2.id选择器 -->
<!-- <style type="text/css">
color: red;
background-color: aquamarine;
}
</style> -->
<!-- 3.class选择器 -->
<!-- <style type="text/css">
.c4.c1{
color: red;
background-color: aqua;
}
</style>-->
<!-- 4.群组选择器 -->
<!-- <style type="text/css">
p,.c1{
color: red;
}
</style> -->
<!-- 5.子代选择器 -->
<!-- <style type="text/css">
div>div>p{
color: red;
}
</style> -->
<!-- 6.后代选择器 -->
<style type="text/css">
div?div p{
color:
}
</style>
<p id="x1">我是段落1</p>
<div id="box">
<p id="x2">我是段落2</p>
<span>我是span1</span>
</div>
<div>
<p id="x3" class="c1 c2 c4">我是段落3</p>
<div>
<p id="x4" class="c2 c4">我是段落4</p>
</div>
<a id="b1" href="">我超链接2</a>
</div>
<a id="b2" href="" class="c1 c3 c4">我是超链接1</a>
</body>
</html>
requests用法
import requests
'''
requests.get(url,*,header,proxies,timeout) - 以指定方式给地址发送网络请求,返回知识服务器返回的响应对象
参数说明:
url - 字符串;请求的网络地址,可能是目标网站网址,也可能是数据接口
header - 字典;请求头,用于伪装浏览器设置user-agent、和完成自动登录设置cookie
proxies - 字典;设置代理ip(ip地址被封的时候使用)
timeout - 数字;设置超时时间
'''
url = "https://search.51job.com/list/090200,000000,0000,00,9,08,%25E6%2595%25B0%25E6%258D%25AE%25E5%2588%2586%25E6%259E%2590,2,1.html?lang=c&postchannel=0000&workyear=01&cotype=99°reefrom=04&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare="
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}
response = requests.get(url,headers)
response.encoding = ''
bs4用法
from bs4 import BeautifulSoup
html = open('05css选择器(重要).html',encoding='utf-8').read()
soup = BeautifulSoup(html,'lxml')
div1 = soup.select_one('#box')
result = div1.select('p')
print(result)
result = soup.select_one('#p2').text
print(result)
print()
result = soup.select_one('#p2').string
print(result)
result = soup.select_one('#p2').contents
print(result)
result = soup.select_one('#a1').attrs['href']
print(result)
作业
import requests
from bs4 import BeautifulSoup
from re import search
import csv
import time
times = 0
all_data = []
while times <= 225:
url = f'https://movie.douban.com/top250?start={times}&filter='
headers = {
'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}
response = requests.get(url,headers=headers)
soup = BeautifulSoup(response.text,'lxml')
all_li = soup.select('.grid_view>li')
for li in all_li:
name_movie = li.select_one('.title').text
result = li.select_one('.bd>p').text.strip().replace(' ', '')
director = search(r'导演:(.+?)\s',result).group(1)
try:
actor = search(r'\s主演:(.+?)\n', result).group(1)
except:
actor = '略'
time_show = search(r'\n(.+?)\s*/',result).group(1)
country = search(r'/\s*(.+?)\s*/',result).group(1)
movie_type = search(r'/.+/\s*(.+?)$',result).group(1)
score = li.select_one('.star>.rating_num').text
people_num_text = li.select('.bd>.star>span')[-1].text
people_num = search(r'([0-9]+).+',people_num_text).group(1)
pic_loction = li.select_one('.pic img').attrs['src']
all_data.append([name_movie,director,actor,time_show,country,movie_type,score,people_num,pic_loction])
times += 25
time.sleep(5)
def fun(list):
writer1 = csv.writer(open('豆瓣电影250.csv','w',encoding='utf-8',newline=''))
writer1.writerow(['电影名字','导演','主演','上映时间','国家','电影类型','评分','评论人数','封面地址'])
writer1.writerows(list)
fun(all_data)