1、序言
在学习完requests网络请求方法和xpath数据解析方法之后,今天通过一个实例来对前面所学的知识进行巩固,也算是一种学以致用吧!
2、代码
# 0、导入所需要的包
import requests
from lxml import etree
# 1、信息的获取
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
,"Referer": "https://movie.douban.com/"
}
url = "https://movie.douban.com/cinema/nowplaying/beijing/"
response = requests.get(url,headers=headers)
text = response.text
# 2、信息的清洗
## 2.1 首先使用etree构造一个实例,方便后续使用xpath进行数据解析
html = etree.HTML(text)
## 2