資源簡介
利用python爬蟲技術(shù)爬取部分城市的旅游信息
代碼片段和文件信息
#-*-?coding?=?utf-8?-*-
#@Time?:?2020/12/23?14:23
#@Author?:?lxy
#@File?:?旅游.py
#@Software:?PyCharm
import?requests
import?parsel
import?csv
for?page?in?range?(130):
????print(“=====================正在爬取第{}頁數(shù)據(jù)==========================“.format(page))
?????#1、得到數(shù)據(jù)所在的url地址
????url=“https://place.qyer.com/china/citylist-0-0-{}/“.format(str(page))
????#2、發(fā)送網(wǎng)絡(luò)請求
????response?=?requests.get(url)
????datalist?=?response.text
????#3、數(shù)據(jù)解析(篩選數(shù)據(jù))
????#3.1轉(zhuǎn)換數(shù)據(jù)
????selector?=?parsel.Selector(datalist)
????#3.2解析數(shù)據(jù)
????list?=?selector.xpath(‘//ul[@class=“plcCitylist“]/li‘)#提取所有l(wèi)i標簽
????for?li?in?list:
????????#城市名
????????travel_place?=?li.xpath(‘.//h3/a/text()‘).get()
????????#拜訪人數(shù)
????????travel_people?=li.xpath(‘.//p[@class=“beento“]/text()‘).get()
????????#熱門景點
????????travel_hot?=?li.xpath(‘.//p[@class=“pois“]/a/text()‘).getall()
????????travel_hot?=?[hot.strip()?for?hot?in?travel_hot]
????????travel_hot=?‘‘.join(travel_hot)
????????#景點鏈接
????????travel_url?=?li.xpath(‘.//h3/a/@href‘).get()
?????????#圖片
????????travel_Imgurl?=?li.xpath(‘./p/a/img/@src‘).get()
????????print(travel_placetravel_peopletravel_hottravel_urltravel_Imgurl)
????????#4、保存數(shù)據(jù)
????????with?open(‘travel.csv‘?mode=‘a(chǎn)‘encoding=‘utf-8‘newline=‘‘)?as?f:
?????????csv_writer?=?csv.writer(f)
?????????csv_writer.writerow([travel_placetravel_peopletravel_hottravel_urltravel_Imgurl])
print(“爬取完畢!“)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????451566??2020-12-28?11:12??python爬蟲爬取旅游信息\data.csv
?????文件????????1605??2020-12-29?16:16??python爬蟲爬取旅游信息\旅游.py
?????目錄???????????0??2020-12-30?22:57??python爬蟲爬取旅游信息\
評論
共有 條評論