python爬取携程旅行网景点评论

您所在的位置:网站首页 携程旅行网评价 python爬取携程旅行网景点评论

python爬取携程旅行网景点评论

2024-07-15 12:34| 来源: 网络整理| 查看: 265

项目简介问题解决全部代码 爬取携程旅行网的景点评论数据,使用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 import ActionChains from selenium.webdriver.common.by import By import time from selenium.webdriver.common.keys import Keys from selenium.webdriver.edge.options import Options 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) 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() # add_argument() 方法添加参数 print(1) with open("dao_chen_ya_ding.txt", "a", encoding='utf-8') as f: for y in range(2,5): time.sleep(3) # to_deal_question() for x in range(10): text = driver.find_elements(By.CLASS_NAME, "commentDetail")[x].text f.write(text) f.write("\n") el = driver.find_element(By.XPATH, '//*[@id="commentModule"]/div[6]/ul/li[{}]/a'.format(y)) # 找到元素 ActionChains(driver).move_to_element(el).click().perform() button = driver.find_element(By.XPATH, '//*[@id="commentModule"]/div[6]/ul/li[{}]/a'.format(y)) button.click() print(y) with open("dao_chen_ya_ding.txt", "a", encoding='utf-8') as f: for y in range(5,300): time.sleep(3) # to_deal_question() # to_view() for x in range(10): text = driver.find_elements(By.CLASS_NAME, "commentDetail")[x].text f.write(text) f.write("\n") el = driver.find_element(By.XPATH, '//*[@id="commentModule"]/div[6]/ul/li[7]/a') # 找到元素 ActionChains(driver).move_to_element(el).click().perform() button = driver.find_element(By.XPATH, '//*[@id="commentModule"]/div[6]/ul/li[7]/a') button.click() print(y) time.sleep(1000) 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('dao_chen_ya_ding_1.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("dao_chen_ya_ding_1_results.txt", "a", encoding='utf-8') as f: for i in range(20): f.write(str(ls[i])) f.write("\n")

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

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

stopwords地址:

python去除文本中的标点符号_去除特殊字符-CSDN博客



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3