Я пытаюсь настроить проект Python Scrapy и запустить его локально на моем ПК.Цель состоит в том, чтобы изучить и понять это.Я включил функцию start_requests () в " main ", но она не вызывается.Любая помощь или ссылки на соответствующие ресурсы, приветствуется.
Программа компилируется без ошибок, но просто открывает пустое окно браузера.Ожидаемый результат - просмотреть список кодов ASIN в .csv и очистить некоторые данные с соответствующих страниц.
# -*- coding: utf-8 -*-
import re
from os.path import splitext, basename
from bs4 import BeautifulSoup as bs
#from scrapy import Spider, Request
country_domain = {'US': {'code': 'us', 'domain': 'com'},
'UK': {'code': 'uk', 'domain': 'co.uk'},
'Germany': {'code': 'de', 'domain': 'de'}, }
def get_asin_url(asin, domain='com'):
#function get_asin_url body emitted for clarity ...
def get_title(soup):
title = ""
pTitle = soup.find('h1', id='title')
if pTitle:
title = re.sub('\s+', ' ', pTitle.text.strip())
return title
class AmazonbotSpider():
print("I'm in class AmazonbotSpider")
name = 'amazonbot'
allowed_domains = ['amazon.*']
start_urls = ['https://amazon.com/']
custom_settings = {'FEED_URI': '%(input_filename)s_%(country)s_%(time)s.csv'}
def __init__(self, asin_path='C:\\Users\\Chris K\Documents\\0_Molzi\\AmazonScraping\\customScripts\\asins.csv', country='UK', *args, **kwargs):
print("I'm in __init__")
super(AmazonbotSpider, self).__init__(*args, **kwargs)
self.asin_path = asin_path
self.country = country
self.country_code = country_domain[country]['domain']
self.input_filename = splitext(basename(asin_path))[0]
with open(self.asin_path, 'r') as fp:
lines = fp.readlines()
for line in lines:
asin = line.strip()
data = get_asin_url(asin, self.country_code)
#data.meta['item'] = {'asin': asin}
print("data: ",data)
#yield data
def start_requests(self):
print("I'm in start_requests")
with open(self.asin_path, 'r') as fp:
lines = fp.readlines()
for line in lines:
asin = line.strip()
data = Request(get_asin_url(asin, self.country_code), callback=self.parse)
data.meta['item'] = {'asin': asin}
print("data: ",data)
yield data
def parse(self, response):
print("I'm in parse")
item = response.meta['item']
soup = bs(response.text, 'lxml')
# Remove any style tags
style_tags = soup.find_all('style')
if style_tags:
for style_tag in style_tags:
style_tag.extract()
item['name'] = get_title(soup)
item['url'] = response.url
yield item
if __name__ == "__main__":
spider = AmazonbotSpider() # Create the object
#spider.start_requests() # Run the rank checker
print("I'm in __main__")