代码小白liu 发表于 2021-7-15 22:09:13

爬虫xpath解析问题求助

import requests
from lxml import etree
url = "https://sc.chinaz.com/ppt/free.html"
headers= {
    "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"
}
response = requests.get(url,headers=headers)
response.encoding = "utf-8"
html_text = response.text
tree = etree.HTML(html_text)
#获取模板名称name和下载地址
div_list = tree.xpath('//div[@class="ppt-listmasonry"]/div')
for div in div_list:
    download_url ="https://sc.chinaz.com/ppt/"+ div.xpath('./div/a/@href')
    name= div.xpath('./div/a/@title')
    print(download_url, name)


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

路神 发表于 2021-7-15 22:42:41

import requests
from lxml import etree

url = "https://sc.chinaz.com/ppt/free.html"
headers = {
    "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"
}
response = requests.get(url, headers=headers)
response.encoding = "utf-8"
html_text = response.text
tree = etree.HTML(html_text)
# 获取模板名称name和下载地址
div_list = tree.xpath('//div[@class="ppt-list "]/div')
for div in div_list:
    download_url = "https://sc.chinaz.com/ppt/" + div.xpath('./div/a/@href')
    name = div.xpath('./div/a/@title')
    print(download_url, name)

冷小漠 发表于 2021-7-16 00:28:56

requests爬下来的HTML源代码你仔细看一下,对应的div的class名称是没有后面一段的,调整一下xpath即可。

代码小白liu 发表于 2021-7-16 19:47:59

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

看到了 谢谢

Kayko 发表于 2021-7-17 07:00:14

{:10_257:}
页: [1]
查看完整版本: 爬虫xpath解析问题求助