首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >已抓取但未抓取的抓取链接

已抓取但未抓取的抓取链接
EN

Stack Overflow用户
提问于 2018-12-18 21:50:24
回答 1查看 107关注 0票数 2

我已经做了一个抓取器来抓取所有与电子商务网站Cdiscount上的"au-quotidien“相关的类别。机器人应该从最上面的菜单开始,然后访问第二层,然后是第三层,然后是抓取项目。以下是我的代码,作为测试:

代码语言:javascript
复制
class CdiscountSpider(scrapy.Spider):
name = "cdis_bot"  # how we have to call the bot
start_urls = ["https://www.cdiscount.com/au-quotidien/v-127-0.html"]

def parse(self, response):
    for link in response.css('div.mvNavSub ul li a::attr(href)').extract():
        regex_top_category = r"\b(?=\w)" + re.escape("au-quotidien") + r"\b(?!\w)"
        if re.search(regex_top_category, link):
            yield response.follow(link, callback = self.parse_on_categories) #going to one layer deep from landing page

def parse_on_categories(self, response):
    for link in response.css('div.mvNavSub ul li a::attr(href)').extract():
        yield response.follow(link, callback = self.parse_on_subcategories) #going to two layer deep from landing page

def parse_on_subcategories(self, response):
    for link in response.css('div.mvNavSub ul li a::attr(href)').extract():
        yield response.follow(link, callback = self.parse_data) #going to three layer deep from landing page

def parse_data(self, response):
    links_list = response.css("div.prdtBILDetails a::attr(href)").extract()
    regex_ean = re.compile(r'(\d+)\.html')
    eans_list = [regex_ean.search(link).group(1) for link in links_list if regex_ean.search(link)]
    desc_list = response.css("div.prdtBILTit::text").extract()
    price_euros = response.css("span.price::text").extract()
    price_cents = response.css("span.price sup::text").extract()
    for euro, cent, ean, desc in zip(price_euros, price_cents, eans_list, desc_list):
        if len(ean) > 6: 
             yield{'ean':ean,'price':euro+cent,'desc':desc,'company':"cdiscount",'url':response.url}

我的问题是,只检索链接。

例如:

代码语言:javascript
复制
2018-12-18 14:40:41 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.cdiscount.com/au-quotidien/alimentaire/pates-riz-/legumes-secs/l-127015303.html> (referer: https://www.cdiscount.com/au-quotidien/alimentaire/pates-riz-/l-1270153.html)
2018-12-18 14:40:41 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.cdiscount.com/au-quotidien/alimentaire/pates-riz-/semoules/l-127015302.html> (referer: https://www.cdiscount.com/au-quotidien/alimentaire/pates-riz-/l-1270153.html)

但是我只得到了很少的几个抓取的项目,总是在相同的类别上,像这样:

代码语言:javascript
复制
{'ean': '2009818241269', 'price': '96€00', 'desc': 'Heidsieck & Co Monopole  75cl x6', 'company': 'cdiscount', 'url': 'https://www.cdiscount.com/vin-champagne/vin-champagne/champagne-brut/l-1293402.html'}
2018-12-18 14:40:34 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.cdiscount.com/vin-champagne/vin-champagne/champagne-brut/l-1293402.html>

而在我看来,其他类别共享相同的项目选择器。

如果你能帮我找出我哪里错了,我将不胜感激:)谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-18 23:32:47

看起来您的parse_data()方法接收到的响应都有很大的不同。

例如,以下是它在示例运行中解析的前三个urls:

代码语言:javascript
复制
https://www.cdiscount.com/vin-champagne/vin-champagne/champagne-millesime/l-1293404.html
https://www.cdiscount.com/vin-champagne/coffrets-cadeaux/v-12960-12960.html
https://www.cdiscount.com/au-quotidien/alimentaire/bio/boisson-bio/jus-de-tomates-bio/l-12701271315.html

很明显(即使快速浏览一下),每个页面的结构都是不同的。

在大多数情况下,您的eans_listdesc_list都为空,因此zip()调用不会产生任何结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53834515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档