关于python简单的爬虫操作(requests和etree)
虎扑体育-NBA球员得分数据排行 第1页
示例代码:
import requests
from lxml import etree
url = 'https://nba.hupu.com/stats/players'
headers = {
"User-Agent": "Mozilla/5.0 (windows NT 10.0; Win64; x64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
res = requests.get(url=url, headers=headers)
print(res)
# 处理请求结果
e = etree.HTML(res.text)
# 解析响应的数据
player = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[2]/a/text()')
team = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[3]/a/text()')
hit_rate = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[6]/text()')[1:]
score = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[4]/text()')[1:]
for p, t, h, s in zip(player, team, hit_rate, score):
print(f"队员:{p},球队:{t},命中率:{h},得分:{s}")
运行结果:
到此这篇关于关于python简单的爬虫操作(requests和etree)的文章就介绍到这了,更多相关python爬虫操作requests和etree内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章