一. 解析html;
安装:pip3 install lxml 引用:from lxml import etree
import requests
# 解析html 安装 pip3 install 1xml
from lxml import etree
res = requests.get(url='http:/.....orders2/?page=1')
# 访问页面
selector = etree.HTML(res.text)
# 取到html页面的内容
count = int(selector.xpath("//p[@class='total']/text()")[0])
# 通过xpath取到总页数,转整数,
limit = 10
if count % limit == 0:
max_page = count / limit
else:
max_page = int(count/limit)+1
# 如果总页数除10是整数,取最大页数;反之 最大页数+1
for i in range(1, max_page+1):
res = requests.get(url='http://123.56.99.53:9003/orders2/?page={i}')
# 循环调这个接口
s = etree.HTML(res.text)
# 将每次取到html页面的内容,装到HTML解释器中,
r = s.xpath('//tr/td[2]/text()')
# 通过xpath 去取需要的值
print(r)
# 打印取到的值
|