前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python爬虫之携程网笔记二

Python爬虫之携程网笔记二

作者头像
python与大数据分析
发布2022-03-11 13:22:54
3310
发布2022-03-11 13:22:54
举报
文章被收录于专栏:python与大数据分析

续昨天的携程网爬虫笔记,昨天只是爬取当页的酒店列表的基本信息,今天是爬取酒店的详细信息,和昨天内容差不多,不过多了一些对字典的操作,将两个字符串合并成一个字典,将两个数组合并成一个字典。

生命在于折腾,仅此而已。

代码如下:

代码语言:javascript
复制
from bs4 import BeautifulSoup
from selenium import webdriver

url='http://hotels.ctrip.com/hotel/436448.html'
hotelid='436448'

def gethoteldetail(hotelid,url):
    driver = webdriver.Chrome(r'D:\Python36\Coding\PycharmProjects\ttt\chromedriver_win32\chromedriver.exe')
    driver.get(url)
    htmlhotelinfo = driver.page_source
    xmlhotelinfo = BeautifulSoup(htmlhotelinfo, 'lxml')

    try:
        hotelspecialinfo = xmlhotelinfo.find("div", class_='special_info').get_text()
    except AttributeError:
        hotelspecialinfo = ''

    try:
        hotelotherinfo = xmlhotelinfo.find("div", class_='htl_room_txt text_3l').find("p").get_text()
    except AttributeError:
        hotelotherinfo = ''

    try:
        hoteltelnum=xmlhotelinfo.find(attrs={"data-real": True})['data-real']
    except AttributeError:
        hoteltelnum = ''

    speciallabels = []
    try:
        speciallabeltag = xmlhotelinfo.find("div", class_="special_label").find_all("i")
        for speciallabel in speciallabeltag:
            speciallabels.append(speciallabel.get_text())
    except AttributeError:
        speciallabels = []

    hotelfacilitys=[]
    try:
        hotelfacilitytag = xmlhotelinfo.find_all("tr",attrs={"data-init": True})
        for hotelfacility in hotelfacilitytag:
            hotelfacilityclass=hotelfacility.find("th").get_text()
            hotelfacilitydetail=hotelfacility.find("td").get_text()
            hotelfacilitydic = {hotelfacilityclass:hotelfacilitydetail}
            hotelfacilitys.append(hotelfacilitydic)
    except AttributeError:
        hotelfacilitys = []

    hotelextracontenclasss=[]
    hotelextracontentdetails=[]
    hotelextracontentsdic=set()
    try:
        hotelextracontenttag = xmlhotelinfo.find("table",class_="detail_extracontent").find_all("th")
        for hotelextracontenclass in hotelextracontenttag:
            hotelextracontenclasss.append(hotelextracontenclass.get_text())
        hotelextracontentdetailtag = xmlhotelinfo.find("table", class_="detail_extracontent").find_all("td")
        for hotelextracontentdetail in hotelextracontentdetailtag:
            hotelextracontentdetails.append(hotelextracontentdetail.get_text())
        hotelextracontentsdic=dict(zip(hotelextracontenclasss,hotelextracontentdetails))
    except AttributeError:
        hotelextracontentsdic = set()
    print('hotel detail=',hotelid,hotelspecialinfo,hotelotherinfo,hoteltelnum,speciallabels,hotelfacilitys,hotelextracontentsdic)

gethoteldetail(hotelid,url)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-03-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python与大数据分析 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档