Debugging 通过Xpath为Scrapy执行简单提取的调试帮助

Debugging 通过Xpath为Scrapy执行简单提取的调试帮助,debugging,xpath,web-scraping,scrapy,Debugging,Xpath,Web Scraping,Scrapy,我试图从这个html中提取公司名称、公司地址和公司编号 <div class="companyInformation"> <p class="contentTitle"> <a itemprop="CompanyName" href='/companies/toshio-s-pte-ltd/'>

我试图从这个html中提取公司名称、公司地址和公司编号

<div class="companyInformation">
                                    <p class="contentTitle">
                                        <a itemprop="CompanyName" href='/companies/toshio-s-pte-ltd/'>
                                            Toshio (S) Pte Ltd
                                        </a>
                                        <input type="hidden" name="data_BasicListings$ctl05$hidBasicComNo" id="data_BasicListings_ctl05_hidBasicComNo" value="T591107709" />
                                    </p>
                                    <p class="address" itemprop="CompanyAddress">

                                        629 Aljunied Rd #08-13 Cititech Ind Bldg S(389838)
                                    </p>
                                    <div class="spriteBtn">
                                        <div class="greyBg">
                                            <span style="display: ">
                                                <a class="phoneBtn" title='+65  67431383#toshio-s-pte-ltd'
                                                    href="#"><span class="phoneLabel">CALL NOW</span> <span class="phoneNum">
                                                        +65  67431383
                                                    </span></a></span><span itemprop="Email" style="display: none">
                                                        <a href='/companies/toshio-s-pte-ltd/#enquiryForm'
                                                            class="enquireBtn"></a></span>
                                        </div>
                                        <span itemprop="Website" style="display: none">
                                            <a rel="nofollow" href='/companies/toshio-s-pte-ltd/'
                                                class="websiteBtn">Home Page</a></span> <span><a class="locationBtn" id='vmap_bs_5'
                                                    target="_blank" rel="nofollow" onclick="_gaq.push(['_trackEvent','Search_Results_Google_Map_Click','click','toshio-s-pte-ltd'])">
                                                    View Map</a></span>
                                    </div>
</div>
然后,我尝试通过以下方式提取公司名称:

item["name"]=names.select("a/@itemprop='CompanyName'").extract()
这是我的全部代码

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from greenbook.items import GreenbookItem

class MySpider(BaseSpider):
    name = "greenbook"
    #allowed_domains = ["craigslist.org"]
    start_urls = ["http://www.thegreenbook.com/products/absorbers-grease-oil/"]

    def parse(self, response):
        hxs=HtmlXPathSelector(response)
        names = hxs.select("//div[@class='companyInformation']")
        items=[]
        for names in names:
            item = GreenbookItem()
            item["name"]=names.select("a/@itemprop='CompanyName'").extract()

        items.append(item)
        return item

我现在什么都没提取,有人能帮我吗??我需要提取公司名称

第二个xpath
a/@itemprop='CompanyName'
应更改为
//a[@itemprop='CompanyName']

我也不认为你是对的。尝试(未测试):


直到稍后我才能进行更深入的检查,但我注意到您的解析函数存在两个问题。首先,您的循环设置是错误的,您还需要返回
items
,而不是
item

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    names = hxs.select("//div[@class='companyInformation']")
    items = []
    for name in names:
        item = GreenbookItem()
        companyName = name.select(".//a[@itemprop='CompanyName']/text()").extract()[0]
        item["name"] = companyName.strip()
        items.append(item)
        return items

我编辑了XPath以仅检索公司名称的文本,并删除了空格。

结果在命令提示符下正确显示在项目列表中,但在csv文件中没有显示。您正在运行什么命令将结果导出到csv文件?scrapy crawl greenbook-o items.csv-t csv对我来说很好。请注意,scrapy将在向其写入输出之前创建文件(然后可打开),此时该文件将为空。您是否尝试在蜘蛛完成爬行后重新打开文件?非常感谢,我想知道为什么我们需要在extract()前面放一个[0],这是什么意思??
 for name in names:
            item = GreenbookItem()
            item["name"]=name.select(".//a[@itemprop='CompanyName']").extract()
def parse(self, response):
    hxs = HtmlXPathSelector(response)
    names = hxs.select("//div[@class='companyInformation']")
    items = []
    for name in names:
        item = GreenbookItem()
        companyName = name.select(".//a[@itemprop='CompanyName']/text()").extract()[0]
        item["name"] = companyName.strip()
        items.append(item)
        return items