爬取携程中评论的数据

爬取携程中评论的数据

1、爬取评论的发布者
2、爬取评论发布的时间
3、爬取评论的内容

在爬取这个携程数据时,将使用selenium自动化的去获取网页数据将网页数据下载下来,使用的是chrom驱动程序,打开网页,如果不会配置,请在评论区提出,我会补录此段:望本文对您有所帮助:

from scrapy import Selector
from selenium import webdriver
import time

# 声明浏览器
browser = webdriver.Chrome ()
browser.get ("URL(请自行补充携程网页地址)")


def parse_page():
    sel = Selector (text=browser.page_source)
    time.sleep (1)

    authors = sel.xpath ('//div[@class="user-date"]/span/text()').extract ()
    # write_times=sel.xpath('//div[@class="user-date"]/span/text()').extract()[i]
    comments = sel.xpath (' //ul[@class="comments"]/li/p/text()').extract ()
    # print (authors)
    # # print(write_times)
    # print (comments)
    author = authors[::3]
    # print (author)
    time_comments = authors[2::3]

    for author, time_comment, comment in zip (author, time_comments, comments):
        with open ('评论.txt', 'a+', encoding='utf-8') as f:
            f.write (
                "评论人:" + author + '\t' + "评论时间" + time_comment + '\t' + "评论内容:" + comment.strip (
                    '\n') + '\n')

    bonwon = browser.find_element_by_xpath ('//ul[@class="pkg_page"]/a[last()]')
    bonwon.click ()

    for i in range (0, 15):
        parse_page ()


if __name__ == '__main__':
    parse_page ()
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值