我在scrapy中編寫了一個代碼來從yellowpage上刮咖啡店。總數據是在870左右,但我有1200左右的重複數量最少。而且,在csv輸出中,數據被放置在每個備用行中。期待某人爲我提供解決方法。提前致謝。在我的scrapy代碼中需要一點點抽搐來擺脫冗餘數據
文件夾名稱 「yellpg」 和 「items.py」 包含
from scrapy.item import Item, Field
class YellpgItem(Item):
name= Field()
address = Field()
phone= Field()
蜘蛛名稱 「yellsp.py」,其中包含:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from yellpg.items import YellpgItem
class YellspSpider(CrawlSpider):
name = "yellsp"
allowed_domains = ["yellowpages.com"]
start_urls = (
'https://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=Los%20Angeles%2C%20CA&page=1',
)
rules = (Rule(LinkExtractor(allow=('\&page=.*',)),callback='parse_item',follow=True),)
def parse_item(self, response):
page=response.xpath('//div[@class="info"]')
for titles in page:
item = YellpgItem()
item["name"] = titles.xpath('.//span[@itemprop="name"]/text()').extract()
item["address"] = titles.xpath('.//span[@itemprop="streetAddress" and @class="street-address"]/text()').extract()
item["phone"] = titles.xpath('.//div[@itemprop="telephone" and @class="phones phone primary"]/text()').extract()
yield item
要獲得CSV輸出,在命令行中我正在使用:
scrapy crawl yellsp -o items.csv
嗨eLRuLL,感謝您的時間回答。重複在這裏沒什麼大不了的。它的數量非常少。也許該網站使用廣告來分析不同的咖啡店,這也是爲什麼數據量比我預期的要多。 – SIM