如何获得用于Scrapy的正确选择器(CSS / XPath)?

问题描述 投票:0回答:2

我想从此网站上抓取信息:https://www.atl.no/finn-trafikkskole?limit=0&limitstart=0(国家驾驶学校列表),以在地图上映射邮政编码和公司名称(我已经从邮政编码映射到坐标),以查找区域。大量的学校。最佳结果将是一个选择器,该选择器提取710个公司中的每个公司的所有相关信息(all relevant information of each company

Highlighted zip code of the first driving school

我已经尝试复制CSS“选择器”和所需表的XPath(table as in Chrome DevTools),但是在Scrapy中运行CSS选择器/ XPath时,它不会返回任何内容。

复制的CSS选择器示例,在Scrapy shell中运行时不显示任何内容:

In(1):response.css("#adminForm > table > tbody").extract()

Out(1):[]

我做错了什么,我应该如何继续获得想要的结果?

css xpath scrapy selector screen-scraping
2个回答
0
投票

基于页面结构,我将拆分工作如下:

    def extract_text(self, item):
        text = item.get()
        text = re.sub(r'<.*?>', '', text)
        return text

    def parse(self, response):
        for school in response.css('.uk-table tr'):

            yield {
                'address': self.extract_text(school.css('.school-address')),
                'school': school.css('tr > td > a::text').get(),
            }

0
投票

[#adminForm > table > tbody返回空结果,因为tbody是由Firefox和Chrome之类的浏览器自动添加的标记。

但是使用Scrapy进行抓取时,响应HTML中没有tbody

请参见页面来源:view-source:https://www.atl.no/finn-trafikkskole?limit=0&limitstart=0

请参阅Scrapy谈论tbody标签https://docs.scrapy.org/en/latest/topics/developer-tools.html#caveats-with-inspecting-the-live-browser-dom

© www.soinside.com 2019 - 2024. All rights reserved.