【爬虫】爬取旅行评论和评分

以马蜂窝“普达措国家公园”为例,其评论高达3000多条,但这3000多条并非是完全向用户展示的,向用户展示的只有5页,数了一下每页15条评论,也就是75条评论,有点太少了吧!

 因此想了个办法尽可能多爬取一些评论,根据我对爬虫爬取数据法律法规的相关了解,爬取看得到的数据是合法的,而在评论最开始的这个地方有对评论的分类,当然每个分类主题也是最多能看到5页内容,但是肯定会比我们被动的只爬取5页多很多内容,因此我们选择按主题分类去爬取评论。

点击上图中的全部,右键检查或者按下F12去定位“全部” 

 把这个元素收起来就可以看到如下图,这个<li></li>标签的列表里保存着分类名称、类型、id等,如果比较多的话可以利用selenium的XPATH自动获取之后,再套进代码里面,由于我只演示一个例子并且分类标签也不多,我就直接拿了这个列表放在代码里。

注意我们需要用到的是他的两个属性值:

data-type、data-catagory

我存放的方式:(代码标注的分类id)

data-type:a = [0,0,1,1,1,2,2,2,2,2,0]
data-catagory:b = [0,2,13,12,11,134700810,173942219,112047583,112968615,143853527,1]

注意这个顺序a[i]b[i]是按照图中框起来的<li></li>标签一一对应的,顺序不能错。

点击Network,按下Ctrl+R刷新一下

找到Name为poiCommentListApi?为首的(如下图),点击Headers,红线画出来的内容是代码中comment_url(代码标注①的地方),根据你自己需要的进行替换。

 下滑可以看到Request Headers中的‘Referer’和‘User-agent’两个参数,根据你自己所需要的进行替换(代码标注的②和③)

 点击Payload,如果是下面这种情况你就点击一下左边的分类标签(任选一个),在Name列表中一直往下滑找到Name为poiCommentListApi?为首的(根据你的点击次数就会有多少个,从后往前找看看规律)

 

找到最后一个Name为poiCommentListApi?为首的,点击Payload,看一下这个params参数

所以对于同一个景点来说,变化的参数有:评论类别(由type、catagory决定)、页码(取值范围1-5)

 

分析完之后就可以写代码了

🌹--<-<-<@美味的code👑 

import re
import time
import requests
import pandas as pd

comment_url = 'http://pagelet.mafengwo.cn/poi/pagelet/poiCommentListApi?'
requests_headers = {
    'Referer': 'https://www.mafengwo.cn/poi/3110.html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
}

# Comment categories
a = [0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 0]
b = [0, 2, 13, 12, 11, 134700810, 173942219, 112047583, 112968615, 143853527, 1]

# Iterate through ten categories of comments
for i in range(11):
    # Get comments from five pages for each category
    for num in range(1, 6):
        print('Fetching Page', num)
        requests_data = {
            'params': '{"poi_id":"3110","type":"%d","category":"%d","page":"%d","just_comment":1}' % (a[i], b[i], num)
        }
        response = requests.get(url=comment_url, headers=requests_headers, params=requests_data)
        
        if 200 == response.status_code:
            page = response.content.decode('unicode-escape', 'ignore').encode('utf-8', 'ignore').decode('utf-8')
            page = page.replace('\\/', '/')
            
            date_pattern = r'<a class="btn-comment _j_comment" title="Add Comment">Comment</a>.*?\n.*?<span class="time">(.*?)</span>'
            date_list = re.compile(date_pattern).findall(page)
            
            star_pattern = r'<span class="s-star s-star(\d)"></span>'
            star_list = re.compile(star_pattern).findall(page)
            
            comment_pattern = r'<p class="rev-txt">([\s\S]*?)</p>'
            comment_list = re.compile(comment_pattern).findall(page)
            
            best_comment = []
            for num in range(0, len(date_list)):
                date = date_list[num]
                star = star_list[num]
                comment = comment_list[num]
                comment = str(comment).replace('&nbsp;', '')
                comment = comment.replace('<br>', '')
                comment = comment.replace('<br />', "")
                comment = comment.replace('\n', "")
                comment = comment.replace("【", "")
                comment = comment.replace("】", "")
                comment = comment.replace("~", "")
                comment = comment.replace("*", "")
                comment = comment.replace('<br />', '')
                best_comment.append(comment)
            df = pd.DataFrame({'date': date_list, 'rating': star_list, 'comment': comment_list})
            df['comment'] = best_comment
            df.to_csv('mafengwo.csv', mode='a', encoding='gb18030', index=False, header=None)
            print('Write successful')
        else:
            print("Fetch failed")

既然都看到這裏了,不如点个关注+收藏再走咯!? 

好的,我可以帮您使用 Python 编写爬虫程序来爬取携程旅行里广州塔的十条评论,并将结果保存到 CSV 文件中。下面是示例代码: 首先,您需要安装 requests beautifulsoup4 库: ``` pip install requests pip install beautifulsoup4 ``` 然后使用以下代码来编写爬虫程序: ```python import csv import requests from bs4 import BeautifulSoup # 设置 User-Agent 头,模拟浏览器访问 HEADERS = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/58.0.3029.110 Safari/537.3'} # 携程旅行广州塔页面 URL url = 'https://you.ctrip.com/sight/guangzhou33/150385.html' # 获取页面 HTML response = requests.get(url, headers=HEADERS) html = response.text # 解析 HTML soup = BeautifulSoup(html, 'html.parser') # 查找评论区 DIV comments_div = soup.find('div', attrs={'class': 'comment_list'}) # 查找评论列表 UL comments_ul = comments_div.find('ul') # 查找所有评论 LI comments_li = comments_ul.findAll('li') # 创建 CSV 文件并写入表头 with open('comments.csv', 'w', encoding='utf-8-sig', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(['评论', '评分', '时间']) # 遍历评论 LI for li in comments_li[:10]: # 只获取前十条评论 # 查找评论内容 DIV content_div = li.find('div', attrs={'class': 'J_commentDetail'}) # 查找评论文本 SPAN text_span = content_div.find('span') # 获取评论文本 text = text_span.text.strip() # 查找评分元素 score_span = li.find('span', attrs={'class': 'TotalScore'}) # 获取评分 score = score_span.text # 查找评论时间元素 time_span = li.find('span', attrs={'class': 'time_line'}) # 获取评论时间 time = time_span['title'].strip() # 将评论评分时间写入 CSV 文件中 writer.writerow([text, score, time]) print('评论爬取完成,结果保存到 comments.csv 文件中。') ``` 运行以上代码,将会在当前目录下生成一个名为 comments.csv 的文件,内容为携程旅行广州塔页面的前十条评论评分时间。 希望这个回答能够解决您的问题,如果您有其他问题,可以继续提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大气层煮月亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值