Python爬虫之携程网笔记三

您所在的位置:网站首页 python爬虫selenium获取携程网机票数据 Python爬虫之携程网笔记三

Python爬虫之携程网笔记三

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

Selenium 的初衷是打造一款优秀的自动化测试工具,但是慢慢的人们就发现,Selenium 的自动化用来做爬虫正合适。

我们知道,传统的爬虫通过直接模拟 HTTP 请求来爬取站点信息,由于这种方式和浏览器访问差异比较明显,很多站点都采取了一些反爬的手段,而 Selenium 是通过模拟浏览器来爬取信息,其行为和用户几乎一样,反爬策略也很难区分出请求到底是来自 Selenium 还是真实用户。而且通过 Selenium 来做爬虫,不用去分析每个请求的具体参数,比起传统的爬虫开发起来更容易。Selenium 爬虫唯一的不足是慢,如果你对爬虫的速度没有要求,那使用 Selenium 是个非常不错的选择。

本文为什么使用Selenium呢?就是因为在爬取携程网的过程中通过传统的urllib.request.urlopen无法识别延迟加载或ajax或动态生成的哪些tag和数据。

所以在第一篇主要使用了urllib.request.urlopen和BeautifulSOAP,在第二篇解析每个酒店的时候使用了selenium 和BeautifulSOAP,在本篇完全没办法解决延迟加载问题,不得已使用了selenium,又嫌在BeautifulSoap之间切换过于麻烦,不得已一边学一边写,使用了更多的特性,比如find_element_by_css_selector、find_element_by_id,也用到了模拟事件点击问题和窗口翻滚,总之一边踩坑一边进步。

对了最重要的一件事就Chrome浏览器的开发者工具,不停的查看一层层的tag。

代码语言:javascript复制import time from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.common.exceptions import NoSuchElementException def gethotelcomment(url): driver = webdriver.Chrome(r'E:\Pycharm\datamine\chromedriver.exe') driver.get(url) #延迟加载,获取当页的广告关闭按钮,点击关闭 time.sleep(2) button = driver.find_element_by_css_selector('#appd_wrap_close') button.click() #获取评论的总页数 try: page = int(driver.find_element_by_id("cTotalPageNum").get_attribute("value")) except: page = 0 commentlists=[] #遍历每个评论页 for m in range(page): #模拟点击按钮,并滚到到屏幕下角 if m: next_button = driver.find_element_by_css_selector('.c_down') next_button.send_keys(Keys.ENTER) driver.execute_script('window.scrollBy(0,5800)') #延迟加载 time.sleep(2) #获取评论细节列表中的每个评论块 commentinforlist = driver.find_element_by_css_selector('.comment_detail_list').find_elements_by_class_name('comment_block') for data in commentinforlist: commentdict = {} #获取评论用户名和用户评论总况 try: commentusername = data.find_element_by_css_selector('div.user_info > p.name').text commentusercommnum = data.find_element_by_css_selector('div.user_info > p.num').text except NoSuchElementException: commentusername='' commentusercommnum = '' #获取用户评论的得分、入住房型、入住时间、出游类型 try: usercommentscore = data.find_element_by_css_selector('div.comment_main > p.comment_title > span.score').text userroomtype = data.find_element_by_css_selector('div.comment_main > p.comment_title > a.J_baseroom_link').text usercheckindate = data.find_element_by_css_selector('div.comment_main > p.comment_title > span.date').text usercheckintype = data.find_element_by_css_selector('div.comment_main > p.comment_title > span.type').text except NoSuchElementException: usercommentscore = '' userroomtype = '' usercheckindate = '' usercheckintype = '' #获取评论细节和评论日期 try: hotelcommenttext = data.find_element_by_css_selector('div.comment_main > div.comment_txt > div.J_commentDetail').text hotelcommentdate = data.find_element_by_css_selector('div.comment_main > div.comment_txt > div.comment_bar > p.comment_bar_info > span.time').text except NoSuchElementException: hotelcommenttext = '' hotelcommentdate = '' commentdict={'username':commentusername,'usercommentnum':commentusercommnum,'commentscore':usercommentscore,'userroomtype':userroomtype, 'usercheckindate':usercheckindate,'usercheckintype':usercheckintype,'hotelcommenttext':hotelcommenttext,'hotelcommentdate':hotelcommentdate} commentlists.append(commentdict) return commentlists if __name__ == '__main__': url = 'http://hotels.ctrip.com/hotel/29922097.html' usercommentlist=gethotelcomment(url) for i in usercommentlist: print(i['hotelcommenttext'])


【本文地址】


今日新闻


推荐新闻


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