想要的效果:
爬取的网站:https://music.163.com/
[图片说明](https://img-ask.csdn.net/upload/202007/13/1594641205_40756.png)
要求返回有多少个dl标签
自己的代码
import scrapy
class music_spider(scrapy.Spider):
name = "music"
start_urls = ['https://music.163.com/']
def parse(self,response):
dl = response.xpath('//body//div[contains(@class,"n-bilst") and contains(@id,"top-flag")]/dl')
print('=' * 20)
print(response.url)
print(len(dl))
print('=' * 20)
输出的dl为空数组,正确的是在列表中应有三个元素
问题:
dl = response.xpath('//body//div[contains(@class,"n-bilst") and contains(@id,"top-flag")]/dl')
返回的dl为空列表
但是把
//body//div[contains(@class,"n-bilst") and contains(@id,"top-flag")]/dl
放在网页源码中搜索会得到三个结果
![图片说明](https://img-ask.csdn.net/upload/202007/13/1594640518_414542.png)
这是为什么?求解!!!