我正在尝试从每个列表中提取企业名称和地址并将其导出到 -csv,但我在输出 csv 时遇到问题。我认为 bizs = hxs.select("//div[@class='listing_content']") 可能会导致问题。
yp_spider.py
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from yp.items import Biz
class MySpider(BaseSpider):
name = "ypages"
allowed_domains = ["yellowpages.com"]
start_urls = ["http://www.yellowpages.com/sanfrancisco/restaraunts"]
def parse(self, response):
hxs = HtmlXPathSelector(response)
bizs = hxs.select("//div[@class='listing_content']")
items = []
for biz in bizs:
item = Biz()
item['name'] = biz.select("//h3/a/text()").extract()
item['address'] = biz.select("//span[@class='street-address']/text()").extract()
print item
items.append(item)
项目.py
# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/topics/items.html
from scrapy.item import Item, Field
class Biz(Item):
name = Field()
address = Field()
def __str__(self):
return "Website: name=%s address=%s" % (self.get('name'), self.get('address'))
'scrapy crawl ypages -o list.csv -t csv' 的输出是一长串企业名称和位置列表,它会多次重复相同的数据。