关于python简单的爬虫操作(requests和etree)
作者:IT之一小佬 时间:2022-01-08 02:17:27
虎扑体育-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}")
运行结果:
来源:https://blog.csdn.net/weixin_44799217/article/details/129786197
标签:python,爬虫,requests,etree
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现的简单计算器功能详解
2023-11-17 09:34:08
![](https://img.aspxhome.com/file/2023/1/64361_0s.jpg)
图文详解如何在WordPress中嵌入iFrame
2023-06-12 23:58:02
![](https://img.aspxhome.com/file/2023/6/55526_0s.png)
Mootools 1.2教程(13)——正则表达式
2008-12-07 20:25:00
表单相关特效整理
2013-06-29 15:42:26
Bootstrap实现渐变顶部固定自适应导航栏
2023-08-23 00:52:40
![](https://img.aspxhome.com/file/2023/8/56078_0s.png)
Jsp生成页面验证码的方法[附代码]
2023-06-25 07:46:42
asp Response.flush 实时显示进度
2011-04-14 10:31:00
HTML5 Canvas 起步(1) - 基本概念
2009-04-21 13:14:00
PHP 页面跳转到另一个页面的多种方法方法总结
2023-06-14 21:05:49
N年前的JAVASCRIPT笔记
2009-01-07 12:40:00
python递归删除指定目录及其所有内容的方法
2022-12-12 02:42:51
BootStrap表单控件之复选框checkbox和单选择按钮radio
2023-08-20 09:36:22
![](https://img.aspxhome.com/file/2023/9/56039_0s.png)
如何对PHP程序中的常见漏洞进行攻击(下)
2023-11-16 14:50:19
使用有趣的自定义标记布局页面
2012-07-12 01:29:03
PHP中把stdClass Object转array的几个方法
2023-11-19 06:56:33
MySQL数据库优化经验详谈
2009-12-15 10:34:00
资料:MsSQL常用SQL语句
2009-02-23 12:54:00
百度首席设计师 用户体验部总监郭宇演讲
2008-09-03 12:41:00
Python实现partial改变方法默认参数
2022-10-30 20:23:33
tensorflow 大于某个值为1,小于为0的实例
2023-06-12 03:00:43