抓取网站上的所有评论与刮痧

2024-06-11 06:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用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。我该怎么做?谢谢您!在


Tags: fromimportcomparse网站response评论filename
1条回答
网友
1楼 · 发布于 2024-06-11 06:00:53

代码如下:

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())

刮痧;输出.txt在

相关问题 更多 >