当前位置:   article > 正文

python简单爬虫操作(requests、etree)_python etree tbody

python etree tbody

虎扑体育-NBA球员得分数据排行 第1页

 示例代码:

  1. import requests
  2. from lxml import etree
  3. url = 'https://nba.hupu.com/stats/players'
  4. headers = {
  5. "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"
  6. }
  7. res = requests.get(url=url, headers=headers)
  8. print(res)
  9. # 处理请求结果
  10. e = etree.HTML(res.text)
  11. # 解析响应的数据
  12. player = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[2]/a/text()')
  13. team = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[3]/a/text()')
  14. hit_rate = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[6]/text()')[1:]
  15. score = e.xpath('//*[@id="data_js"]/div[4]/div/table/tbody/tr/td[4]/text()')[1:]
  16. for p, t, h, s in zip(player, team, hit_rate, score):
  17. print(f"队员:{p},球队:{t},命中率:{h},得分:{s}")

运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/924365
推荐阅读
相关标签
  

闽ICP备14008679号