当类包含空格时获取空数组

2024-03-29 01:49:49 发布

您现在位置:Python中文网/ 问答频道 /正文

Python 2.7版

我想获取每个新的背景图像url和标题,但是当我尝试获取图像url时,我总是使用xpath获取空数组。你知道吗

以下是我尝试的:

scrapy shell http://www.wownews.tw/fashion/movie

然后呢

response.body

我可以在终端上看到html数据。但是当我打字的时候

response.xpath('//div[@class="text ng-scope"]')

得到空数组,我觉得应该是工作。你知道吗

问题是因为类包含空格吗?你知道吗

如何修复?任何帮助都将不胜感激。你知道吗

我试过命令还是得到空数组

response.xpath('//div[contains(concat(" ", normalize-space(@class), " "), "text ng-scope")]')

Tags: text图像divhttpurl标题response数组
1条回答
网友
1楼 · 发布于 2024-03-29 01:49:49

这是你需要的一切

import json
import scrapy


class ListingSpider(scrapy.Spider):
    name = 'listing'

    start_urls = ['http://api.wownews.tw/f/pages/site/558fd617913b0c11001d003d?category=5590a6a3f0a8bf110060914d&children=true&limit=48&page=1']

    def parse(self, response):
        items = json.loads(response.body)['results']

        for item in items:
            yield item

参考https://medium.com/@yashpokar/scrape-any-website-in-the-internet-without-using-splash-or-selenium-68a6c9733369

相关问题 更多 >