鱼C论坛

 找回密码
 立即注册
查看: 2302|回复: 4

[已解决]爬虫xpath解析问题求助

[复制链接]
发表于 2021-7-15 22:42:41 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. from lxml import etree

  3. url = "https://sc.chinaz.com/ppt/free.html"
  4. headers = {
  5.     "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Mobile Safari/537.36"
  6. }
  7. response = requests.get(url, headers=headers)
  8. response.encoding = "utf-8"
  9. html_text = response.text
  10. tree = etree.HTML(html_text)
  11. # 获取模板名称name和下载地址
  12. div_list = tree.xpath('//div[@class="ppt-list "]/div')
  13. for div in div_list:
  14.     download_url = "https://sc.chinaz.com/ppt/" + div.xpath('./div[2]/a/@href')[0]
  15.     name = div.xpath('./div[2]/a/@title')[0]
  16.     print(download_url, name)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-23 07:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表