Сохраните Scrapy 'start_urls' и сохраните правильно в фрейме данных - PullRequest
0 голосов
/ 01 апреля 2019

Я использую Scrapy, чтобы очистить некоторые данные сайта.Но я не могу сделать шаг, чтобы получить мои данные правильно.

Это вывод моего кода (см. Код ниже):

В командной строке:

scrapy crawl myspider -o items.csv

Вывод:

asin_product                                    product_name
ProductA,,,ProductB,,,ProductC,,,            BrandA,,,BrandB,,,BrandC,,,    

ProductA,,,ProductD,,,ProductE,,,            BrandA,,,BrandB,,,BrandA,,,    

#Note that the rows are representing the start_urls and that the ',,,' 
#three commas are separating the data.  

Желаемый вывод:

scrapy crawl myspider -o items.csv

Start_URL     asin_product      product_name 
URL1           ProductA           BrandA
URL1           ProductB           BrandB
URL1           ProductC           BrandC
URL2           ProductA           BrandA
URL2           ProductD           BrandB
URL2           ProductE           BrandA

Мой использованный код в Scrapy:

import scrapy
from amazon.items import AmazonItem

class AmazonProductSpider(scrapy.Spider):
  name = "AmazonDeals"
  allowed_domains = ["amazon.com"]

#Use working product URL below
   start_urls = [
      "https://www.amazon.com/s?k=shoes&ref=nb_sb_noss_2",   # This should 
       be #URL 1       
      "https://www.amazon.com/s?k=computer&ref=nb_sb_noss_2" # This should 
       be #URL 2 
 ]

def parse(self, response):

  items = AmazonItem()

  title = response.xpath('//*[@class="a-size-base-plus a-color-base a- 
  text-normal"]/text()').extract()
  asin =  response.xpath('//*[@class ="a-link-normal"]/@href').extract()  

  # Note that I devided the products with ',,,' to make it easy to separate 
  # them. I am aware that this is not the best approach. 
  items['product_name'] = ',,,'.join(title).strip()
  items['asin_product'] = ',,,'.join(asin).strip()

  yield items

Ответы [ 2 ]

1 голос
/ 01 апреля 2019

Прежде всего, рекомендуется использовать css при запросах по классу .

Теперь к вашему коду:

Название продукта находится внутри тега (URL продукта). Таким образом, вы можете перебирать ссылки и сохранять URL-адрес и заголовок.

<a class="a-link-normal a-text-normal" href="/adidas-Mens-Lite-Racer-Running/dp/B071P19D3X/ref=sr_1_3?keywords=shoes&amp;qid=1554132536&amp;s=gateway&amp;sr=8-3">
    <span class="a-size-base-plus a-color-base a-text-normal">Adidas masculina Lite Racer byd tênis de corrida</span>
</a>   

Вам нужно создать один AmazonItem объект на строку в вашем CSV-файле.

def parse(self, response):

    # You need to improve this css selector because there are links which
    # are not a product, this is why I am checking if title is None and continuing.
    for product in response.css('a.a-link-normal.a-text-normal'):
        # product is a selector
        title = product.css('span.a-size-base-plus.a-color-base.a-text-normal::text').get()
        if not title:
            continue
        # The selector is already the a tag, so we only need to extract it's href attribute value.
        asin =  product.xpath('./@href').get()

        item = AmazonItem()
        item['product_name'] = title.strip()
        item['asin_product'] = asin.strip()

        yield item
1 голос
/ 01 апреля 2019
  1. Сделать start_url доступным в методе разбора

вместо использования start_urls, вы можете выдавать свои начальные запросы из метода с именем start_requests (см. https://docs.scrapy.org/en/latest/intro/tutorial.html?highlight=start_requests#our-first-spider).

С каждым запросом вы можете передавать стартовый URL как метаданные. Эти метаданные затем доступны в вашем методе разбора (см. https://docs.scrapy.org/en/latest/topics/request-response.html?highlight=meta#scrapy.http.Request.meta).

def start_requests(self):
    urls = [...]  # this is equal to your start_urls
    for start_url in urls:
        yield Request(url=url, meta={"start_url": start_url})

def parse(self, response):
    start_url = response.meta["start_url"]
дает несколько элементов, по одному для каждого продукта

Вместо объединения названий и брендов вы можете получить несколько элементов из анализа.Для примера ниже я предполагаю, что заголовки списков и asin имеют одинаковую длину.

for title, asin in zip(title, asin):
    item = AmazonItem()
    item['product_name'] = title
    item['asin_product'] = asin
    yield item 

PS: вы должны проверить amazons robots.txt.Они могут не позволить вам очистить их сайт и забанить ваш IP (https://www.amazon.de/robots.txt)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...