我正在尝试使用Scrapy抓取购物网站上的所有评论。我找到了这段代码:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class deneme(CrawlSpider):
name = 'deneme'
allowed_domains = ['hepsiburada.com']
start_urls = ['https://www.hepsiburada.com/']
rules = (
Rule(LinkExtractor(), callback='parse_item', follow=True),
)
def parse_item(self, response):
filename = response.url.split("/")[-2] + '.html'
with open(filename, 'wb') as f:
f.write(response.body)
此代码抓取所有网站。但我想只抓取网站上的评论并将其写入MongoDB数据库。我不擅长Python。我怎样才能做到这一点?谢谢!
答案 0 :(得分:-2)
以下是代码:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class deneme(CrawlSpider):
name = 'yorum'
allowed_domains = ['hepsiburada.com']
start_urls = ['https://www.hepsiburada.com/']
rules = (
Rule(LinkExtractor(), callback='parse_item', follow=True),
)
def parse_item(self, response):
print(response.xpath('//p[@class="review-text"]/text()').extract())
scrapy crawl yorum> output.txt的