import requests
from bs4 import BeautifulSoup
import pandas as pd
from pandas import DataFrame
url="https://travelsearch.fliggy.com/index.htm?keyword=%E6%B3%89%E5%B7%9E%E7%9A%84%E9%85%92%E5%BA%97&searchType=MULTI_SEARCH&fromSug=1"#爬取飞猪泉州酒店信息
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}#
r=requests.get(url)#请求网站
r.encoding=r.apparent_encoding#统一编码
data=r.text
soup=BeautifulSoup(data,'html.parser')#使用“美味的汤”工具
print(soup.prettify())#显示网站结构
hotel=[]#建立空列表
price=[]
house=[]
for i in soup.find_all(class_="main-title"):#把酒店名称添加进空列表
    hotel.append(i.get_text().strip())
for k in soup.find_all(class_="price"):#把酒店价格添加进空列表
    price.append(k.get_text().strip())
for g in soup.find_all(class_="hotel-star"):#把酒店的房间型/星级/最近的标志性地区添加进空列表
    house.append(g.get_text().strip())
data=[hotel,house,price]
print(data)
s=pd.DataFrame(data,index=["标题","房间型/星级/最近的标志性地区","价格"])
print(s.T)#数据可视化
 
 
1.导入相应爬虫工具
2.按f12获得源代码

 

 

找到要爬取的数据

用代码进行爬取

最终得到:

 

 

posted on 2020-03-19 13:12  唐灿杰  阅读(199)  评论(0编辑  收藏  举报