如何使用Scrapy Python从重定向链接中提取网站URL

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

我编写了一个脚本来从网站获取数据。由于@href是重定向链接,因此我在收集网站URL时遇到问题。如何将重定向URL转换为重定向到的实际网站?

import scrapy
import logging


class AppSpider(scrapy.Spider):
    name = 'app'
    allowed_domains = ['www.houzz.in']
    start_urls = ['https://www.houzz.in/professionals/searchDirectory?topicId=26721&query=Design-Build+Firms&location=Mumbai+City+District%2C+India&distance=100&sort=4']

    def parse(self, response):
        lists = response.xpath('//li[@class="hz-pro-search-results__item"]/div/div[@class="hz-pro-search-result__info"]/div/div/div/a')
        for data in lists:
            link = data.xpath('.//@href').get()

            yield scrapy.Request(url=link, callback=self.parse_houses, meta={'Links': link})

        next_page = response.xpath('(//a[@class="hz-pagination-link hz-pagination-link--next"])[1]/@href').extract_first()
        if next_page:
            yield response.follow(response.urljoin(next_page), callback=self.parse)

    def parse_houses(self, response):
        link = response.request.meta['Links']

        firm_name = response.xpath('//div[@class="hz-profile-header__title"]/h1/text()').get()
        name = response.xpath('//div[@class="profile-meta__val"]/text()').get()
        phone = response.xpath('//div[@class="hz-profile-header__contact-info text-right mrm"]/a/span/text()').get()
        website = response.xpath('(//div[@class="hz-profile-header__contact-info text-right mrm"]/a)[2]/@href').get()

        yield {
            'Links': link,
            'Firm_name': firm_name,
            'Name': name,
            'Phone': phone,
            'Website': website
        }
python web-scraping scrapy
2个回答
0
投票

您可以这样做:

class AppSpider(scrapy.Spider):
    base_url = 'www.houzz.in{}'
    .
    .
    .
    def foo(self):
        actual_url = self.base_url.format(redirect_url)

0
投票

您必须向该目标URL发出请求,以查看其指向的位置

在您的情况下,您可以简单地执行HEAD请求,该请求不会加载目标URL的任何正文,因此也可以节省带宽并提高脚本速度]]

class AppSpider(scrapy.Spider):
    name = 'app'
    allowed_domains = ['www.houzz.in']
    start_urls = ['https://www.houzz.in/professionals/searchDirectory?topicId=26721&query=Design-Build+Firms&location=Mumbai+City+District%2C+India&distance=100&sort=4']

    def parse(self, response):
        lists = response.xpath('//li[@class="hz-pro-search-results__item"]/div/div[@class="hz-pro-search-result__info"]/div/div/div/a')
        for data in lists:
            link = data.xpath('.//@href').get()

            yield scrapy.Request(url=link, callback=self.parse_houses, meta={'Links': link})

        next_page = response.xpath('(//a[@class="hz-pagination-link hz-pagination-link--next"])[1]/@href').extract_first()
        if next_page:
            yield response.follow(response.urljoin(next_page), callback=self.parse)

    def parse_houses(self, response):
        link = response.request.meta['Links']

        firm_name = response.xpath('//div[@class="hz-profile-header__title"]/h1/text()').get()
        name = response.xpath('//div[@class="profile-meta__val"]/text()').get()
        phone = response.xpath('//div[@class="hz-profile-header__contact-info text-right mrm"]/a/span/text()').get()
        website = response.xpath('(//div[@class="hz-profile-header__contact-info text-right mrm"]/a)[2]/@href').get()

        yield Request(url=website, 
            method="HEAD", 
            callback=self.get_final_link,
            meta={'data': 
                    {
                    'Links': link,
                    'Firm_name': firm_name,
                    'Name': name,
                    'Phone': phone,
                    'Website': website
                }
            }
            )


    def get_final_link(self, response):
        data = response.meta['data']
        data['website'] = response.headers['Location']
        yield data
© www.soinside.com 2019 - 2024. All rights reserved.