Python 如果缺少一个项目,则不刮

Python 如果缺少一个项目,则不刮,python,python-3.x,scrapy,Python,Python 3.x,Scrapy,在过去的两天里,我在几个小时内构建了我的第一个scray spider,但我现在陷入了困境——我想实现的主要目的是提取所有数据,以便稍后在csv中对其进行过滤。现在,对我来说真正重要的数据(没有!网页的公司)被删除了,因为如果一个项目有主页,scrapy找不到我提供的xpath。我在这里尝试了if语句,但它不起作用 示例网站: 我使用xPath选择器:response.xPath(//div[@class='cCore\u contactInformationBlockWithIcon cCor

在过去的两天里,我在几个小时内构建了我的第一个scray spider,但我现在陷入了困境——我想实现的主要目的是提取所有数据,以便稍后在csv中对其进行过滤。现在,对我来说真正重要的数据(没有!网页的公司)被删除了,因为如果一个项目有主页,scrapy找不到我提供的xpath。我在这里尝试了if语句,但它不起作用

示例网站:

我使用xPath选择器:
response.xPath(//div[@class='cCore\u contactInformationBlockWithIcon cCore\u wwwIcon']/a/@href”).extract()

非网站示例:

蜘蛛代码:

# -*- coding: utf-8 -*-
import scrapy

class AchernSpider(scrapy.Spider):
name = 'achern'
allowed_domains = ['www.achern.de']
start_urls = ['https://www.achern.de/de/Wirtschaft/Unternehmen-A-Z/']



def parse(self, response):
    for href in response.xpath("//ul[@class='cCore_list cCore_customList']/li[*][*]/a/@href"):
        url = response.urljoin(href.extract())
        yield scrapy.Request(url, callback= self.scrape)

def scrape(self, response):
    #Extracting the content using css selectors
    print("Processing:"+response.url)
    firma = response.css('div>#cMpu_publish_company>h2.cCore_headline::text').extract()
    anschrift = response.xpath("//div[contains(@class,'cCore_addressBlock_address')]/text()").extract()
    tel = response.xpath("//div[@class='cCore_contactInformationBlockWithIcon cCore_phoneIcon']/text()").extract()
    mail = response.xpath(".//div[@class='cCore_contactInformationBlock']//*[contains(text(), '@')]/text()").extract()
    web1 = response.xpath("//div[@class='cCore_contactInformationBlockWithIcon cCore_wwwIcon']/a/@href").extract()
    if "http:" not in web1:
        web = "na"
    else:
        web = web1

    row_data=zip(firma,anschrift,tel,mail,web1) #web1 must be changed to web but then it only give out "n" for every link
    #Give the extracted content row wise
    for item in row_data:
        #create a dictionary to store the scraped info
        scraped_info = {
            'Firma' : item[0],
            'Anschrift' : item[1] +' 77855 Achern',
            'Telefon' : item[2],
            'Mail' : item[3],
            'Web' : item[4],
        }

        #yield or give the scraped info to scrapy
        yield scraped_info
因此,总的来说,即使没有“web”,它也应该导出删除的项目

希望有人能帮忙,用英语问候S

response.css(".cCore_wwwIcon > a::attr(href)").get()
为您提供
None
或网站地址,然后您可以使用
提供默认值:

website = response.css(".cCore_wwwIcon > a::attr(href)").get() or 'na'
另外,我重构了你的刮板来使用css选择器。请注意,我使用了
.get()
而不是
.extract()

import scrapy
从scrapy.crawler导入crawler进程
等级AchernSpider(刮毛蜘蛛):
名称='achern'
允许的_域=['www.achern.de']
起始URL=['https://www.achern.de/de/Wirtschaft/Unternehmen-A-Z/']
def解析(自我,响应):
对于response.css中的url(“[class*=cCore\u listRow]>a::attr(href)”).extract():
生成scrapy.Request(url,callback=self.scrape)
def刮伤(自身,响应):
#使用css选择器提取内容
firma=response.css('.cCore_headline::text').get()
anschrift=response.css('.cCore\u addressBlock\u address::text').get()
tel=response.css(“.cCore\u phoneIcon::text”).get()
mail=response.css(“[href^=mailto]::attr(href)”).get().replace('mailto:','')
website=response.css(“.cCore\u wwwIcon>a::attr(href)”).get()或“na”
刮取的_信息={
“菲尔玛”:菲尔玛,
'Anschrift':Anschrift+'77855 Achern',
电话:,
“邮件”:邮件,
"网站":,
}
产量信息
如果名称=“\uuuuu main\uuuuuuuu”:
p=爬网进程()
p、 爬行(蜘蛛)
p、 开始()
输出:

with website:
{'Firma': 'Wölfinger Fahrschule GmbH', 'Anschrift': 'Güterhallenstraße 8 77855 Achern', 'Telefon': '07841 6738132', 'Mail': 'info@woelfinger-fahrschule.de', 'Web': 'http://www.woelfinger-fahrschule.de'}

without website:
{'Firma': 'Zappenduster-RC Steffen Liepe', 'Anschrift': 'Am Kirchweg 16 77855 Achern', 'Telefon': '07841 6844700', 'Mail': 'Zappenduster-Rc@hotmail.de', 'Web': 'na'}

该死,太快了。非常感谢你。。它只是“或”-它就像一个符咒!格恩·格谢恩:)