鱼C论坛

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

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

[复制链接]
发表于 2021-7-15 22:09:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  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  masonry"]/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)
复制代码


为什么打印出来没数据那?提示:Process finished with exit code 0

最佳答案
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
回复

使用道具 举报

发表于 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
回复 支持 反对

使用道具 举报

发表于 2021-7-16 00:28:56 | 显示全部楼层
requests爬下来的HTML源代码你仔细看一下,对应的div的class名称是没有后面一段的,调整一下xpath即可。

html源码中的class

html源码中的class

调整class名称后可提取出链接

调整class名称后可提取出链接
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-16 19:47:59 | 显示全部楼层
冷小漠 发表于 2021-7-16 00:28
requests爬下来的HTML源代码你仔细看一下,对应的div的class名称是没有后面一段的,调整一下xpath即可。

看到了 谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-17 07:00:14 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 18:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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