Hold_C
Hold_C
采纳率100%
2020-07-13 19:48

【scrapy爬虫问题】scrapy.xpath解析返回的结果有问题,求解答!!!

已采纳

想要的效果:

爬取的网站: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)
这是为什么?求解!!!
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq_43656607 放风喽 9月前

    先打印response看看和你在网页上看到的一样吗??

    点赞 评论 复制链接分享