前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
今年的國慶節還有半個月就要來了,相信很多的小伙伴還是非常期待這個小長假的,國慶節是一年中的小長假,很多的朋友會選擇旅行來度過,中國的旅游城市有很多,旅游景點也是數不勝數,
那么,2020國內十一國慶適合去哪里游玩呢?
專案目標
爬取美團旅游景點評論
受害者網址
[https://chs.meituan.com/](https://chs.meituan.com/)
代碼
安裝庫
pip install requests
pip install time
匯入工具
import requests import time
決議網站,爬取資料
for page in range(10, 101, 10): time.sleep(1) url = 'https://www.meituan.com/ptapi/poi/getcomment?id=1161635&offset=0&pageSize={}&sortType=1'.format(page) headers = { 'Host': 'www.meituan.com', 'Pragma': 'no - cache', 'Referer': 'https: // www.meituan.com / zhoubianyou / 1161635 /', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=url, headers=headers) html_data = response.json() info_data = html_data['comments'] for i in info_data: info = i['comment']
保存資料
for i in info_data: info = i['comment'] with open('美團評論.csv', mode='a', encoding='utf-8-sig', newline='') as f: f.write(info) print(info)
運行代碼,效果如下圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/68314.html
標籤:Python
上一篇:python 求取代碼運行時間
