当前位置: 首页 > news >正文

python爬取穷游网景点评论

爬取穷游网的景点评论数据,使用selenium爬取edge浏览器的网页文本数据。

同程的评论数据还是比较好爬取,不像大众点评需要你登录验证杂七杂八的,只需要找准你想要爬取的网页链接就能拿到想要的文本数据。

这里就不得不提一下爬取过程中遇到的问题,就是关于无头模式和有头模式,首先介绍一下什么是无头模式和有头模式:

无头模式和有头模式是指网络爬虫在执行过程中是否显示浏览器的界面。

有头模式是指网络爬虫在执行过程中会显示浏览器的界面,可以看到爬取过程中的页面加载、点击等操作,可以进行人工干预和调试。有头模式一般用于开发和调试阶段,便于观察爬虫的执行情况。

无头模式是指网络爬虫在执行过程中不显示浏览器的界面,所有的操作都在后台进行,不会干扰用户的正常使用。无头模式一般用于实际的爬取任务,可以提高爬取效率,减少资源消耗。

总的来说,无头模式和有头模式的区别在于是否显示浏览器界面,有头模式适用于开发和调试阶段,无头模式适用于实际的爬取任务。

无头模式的问题:

1、无头模式下缺少浏览器信息,或默认填充的浏览器信息带有爬虫痕迹,会被识别为机器人而导致爬虫执行失败。

2、页面动态加载时,有时会根据页面size来布局控件,如果size太小会出现控件加载失败情况。

所以经常爬到二十多页的时候就突然报错“找不到元素无法点击”这种的错误。又或者是爬到三十多页又告诉我找不到元素,某某列表为空,就很烦。😠 😡 😤

为了解决这个问题我的尝试:

1:延长页面的存在的时间,让服务器充分响应,并且模拟手下拉的操作,让下面没显示出来的界面加载出来:

def to_the_buttom():js = 'document.getElementsByClassName("search-body left_is_mini")[0].scrollTop=10000'driver.execute_script(js)
def to_the_top():js = "var q=document.documentElement.scrollTop=0"  # 滚动到最上面driver.execute_script(js)
def to_deal_question():driver.implicitly_wait(10)time.sleep(3)to_the_buttom()time.sleep(3)
def to_view():driver.implicitly_wait(10)to_the_buttom()time.sleep(3)button = driver.find_element(By.XPATH, '//*[@id="commentModule"]/div[6]/ul/li[7]/a')driver.execute_script("arguments[0].scrollIntoView();", button)

2:使用Selenium库中的webdriver来实例化一个Microsoft Edge浏览器的驱动程序,并设置了一些选项。

opt = Options()
opt.add_argument("--headless")
opt.add_argument("window-size=1920x1080")
opt.add_argument('--start-maximized')
driver = webdriver.Edge(options=opt)
url = 'https://you.ctrip.com/sight/daocheng342/11875.html'
driver.get(url)
# driver.maximize_window()

然后就可以愉快把评论全拿到手了,这里是穷游网木格措的评论。

最后我还用jieba库做了一下词条分析,想看看这个景点大家的关注点都是些什么。

全部代码:

爬取数据板块:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
from requests import request
from selenium.webdriver.support import ui
from selenium.webdriver.support.wait import WebDriverWaitdriver = webdriver.Edge()
url = 'https://place.qyer.com/poi/V2UJZ1FgBzZTYVI2/'
driver.implicitly_wait(10)
driver.get(url)
driver.maximize_window()
def to_the_buttom():js="var q=document.documentElement.scrollTop=100000"driver.execute_script(js)with open("mu_ge_cuo_2.txt", "a", encoding='utf-8') as f:for x in range(1,6):driver.implicitly_wait(10)to_the_buttom()time.sleep(3)to_the_buttom()for i in range(1,11):text=driver.find_element(By.XPATH, "/html/body/div/div/div[2]/div/div[4]/div/div[2]/div[1]/div[2]/div[2]/ul/li[{}]/div/p".format(i)).textf.write(text)f.write("\n")print(x)button = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div[4]/div/div[2]/div[1]/div[2]/div[2]/div[1]/div/a[{}]'.format(x))button.click()
# with open("mu_ge_cuo_2.txt", "a", encoding='utf-8') as f:
#     for x in range(6,83):
#         driver.implicitly_wait(10)
#         to_the_buttom()
#         time.sleep(3)
#         to_the_buttom()
#         for i in range(1,11):
#             text=driver.find_element(By.XPATH, "/html/body/div/div/div[2]/div/div[4]/div/div[2]/div[1]/div[2]/div[2]/ul/li[{}]/div/p".format(i)).text
#             f.write(text)
#             f.write("\n")
#         print(x)
#         button = driver.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div[4]/div/div[2]/div[1]/div[2]/div[2]/div[1]/div/a[6]')
#         button.click()time.sleep(100000)
driver.close()

分析数据提取词条板块:

import jieba
stopwords = [line.strip() for line in open('hit_stopwords.txt',encoding='utf-8').readlines()]
stopwords.append("\n")
# print(stopwords)
f1=open('mu_ge_cuo_2.txt','r',encoding='utf-8')
code=[]
for i in f1.read().strip().split(' '):words = jieba.lcut(i)code+=words
d={}
for word in code:if word not in stopwords:d[word]=d.get(word,0)+1
ls=list(d.items())
ls.sort(key=lambda s:s[-1],reverse=True)
print(ls)
f1.close()
with open("mu_ge_cuo_2_results.txt", "a", encoding='utf-8') as f:for i in range(20):f.write(str(ls[i]))f.write("\n")

里面的stopwords是为了去除标点符号、特殊字符和语气助词,在主页的其他文章里有提供。

如果这篇文章能对您有所帮助的话,还望点个赞赞呀~😘

http://www.lryc.cn/news/233425.html

相关文章:

  • Phar 文件上传以及反序列化
  • 面试其他注意事项
  • sklearn 笔记 BallTree/KD Tree
  • ConstraintLayout使用详解
  • Java8Stream快速使用
  • work环境配置
  • Flutter应用-使用sqflite升级数据库
  • 集群搭建(redis7)
  • 高能分享:软件测试十大必问面试题(附带答案)
  • Java 反射设置List属性
  • wpf devexpress Property Grid创建属性定义
  • 78.子集--77.组合
  • 【C++】模版-初阶
  • 【JavaEE初阶】 TCP服务器与客户端的搭建
  • 23111710[含文档+PPT+源码等]计算机毕业设计基于SpringBoot的体育馆场地预约赛事管理系统的设计
  • 【论文解读】GPT Understands, Too
  • 组合式API_生命周期
  • WPF如何实现应用程序托盘
  • ERROR: column “xxxx.id“ must appear in the GROUP BY
  • 【C++ 学习 ㊲】- 五种特殊类的设计
  • 探索arkui(2)--- 布局(列表)--- 2(支持分组/实现响应滚动位置)
  • systemverilog:interface中端口方向理解
  • 【GUI】-- 08 JButton、JRadioButton、JCheckBox
  • 【postgresql】CentOS7 安装Pgweb
  • 基于python和定向爬虫的商品比价系统
  • 使用GPT-4训练数据微调GPT-3.5 RAG管道
  • 二十三种设计模式全面解析-深入解析模板方法模式的奇妙世界
  • 【Spring】加载properties文件
  • react中间件的理解
  • React函数组件状态Hook—useState《进阶-对象数组》