爬虫xpath解析问题求助
import requestsfrom 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
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)
requests爬下来的HTML源代码你仔细看一下,对应的div的class名称是没有后面一段的,调整一下xpath即可。 冷小漠 发表于 2021-7-16 00:28
requests爬下来的HTML源代码你仔细看一下,对应的div的class名称是没有后面一段的,调整一下xpath即可。
看到了 谢谢 {:10_257:}
页:
[1]