request`
requests的作用
作用:发送网络请求,返回响应数据
requests的作用
作用:发送网络请求,返回响应数据```python
response =requests.get(‘http://www.baidu.com’)
response.encoding=‘utf8’
print(response.text)
print(response.encoding)
`
BeautifulSoup
find方法
查找文档中的标签或者文档中的数据
![在这里插入图片描述](https://img-blog.csdnimg.cn/3d8c7f2e7a314e4da647f05fffa37eef.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmU5biD5pav55qE5bC-,size_16,color_FFFFFF,t_70,g_se,x_16)
tag对象:
name:获取标签名称
attrs:获取标签所有的值
text:获取标签所有的字符串
![在这里插入图片描述](https://img-blog.csdnimg.cn/f0414b8c5a9f461abfe67682197b2ab7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmU5biD5pav55qE5bC-,size_20,color_FFFFFF,t_70,g_se,x_16)
![在这里插入图片描述](https://img-blog.csdnimg.cn/3193d1b237ed4195997f06ad0164a970.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LmU5biD5pav55qE5bC-,size_20,color_FFFFFF,t_70,g_se,x_16)
运用find和request搜找疫情页面信息
```python
import requests
from bs4 import BeautifulSoup
response=requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
home_page=response.content.decode()
#print(home_page)
soup=BeautifulSoup(home_page,'lxml')
script=soup.find(id='getListByCountryTypeService2true')
text=script.string
print(text)
正则表达式:
提取疫情页面中的json模块
import requests
from bs4 import BeautifulSoup
import re
response=requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
page=response.content.decode()
soup=BeautifulSoup(page,'lxml')
script=soup.find(id='getListByCountryTypeService2true')
text=script.string
print(text)
#使用正则表达式导入Json表达式
json_str=re.findall(r'.\[.+\]',text[0])
print(json_str)import requests
from bs4 import BeautifulSoup
import re
response=requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
page=response.content.decode()
soup=BeautifulSoup(page,'lxml')
script=soup.find(id='getListByCountryTypeService2true')
text=script.string
print(text)
#使用正则表达式导入Json表达式
json_str=re.findall(r'.\[.+\]',text[0])
print(json_str)import requests
from bs4 import BeautifulSoup
import re
response=requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')
page=response.content.decode()
soup=BeautifulSoup(page,'lxml')
script=soup.find(id='getListByCountryTypeService2true')
text=script.string
print(text)
#使用正则表达式导入Json表达式
json_str=re.findall(r'.\[.+\]',text[0])
print(json_str)