兄弟们,我来了!今天整个好玩的,你们肯定喜欢~
咱们上班累了,不得好好犒劳一下自己,是吧,于是我整了一手爬取附近洗jio的店子,浴皇大帝们,冲鸭!
话不多说,冲!兄弟们,都是正规的 正规的!
用的环境是
python 3.8 解释器
pycharm 编辑器
用的大多数的知识点 都是属于基础的知识点内容,以及爬虫基础入门一些知识点。
要用的模块
requests >>> pip install requests 第三方模块 需要大家去安装
csv
win + R 输入cmd 输入安装命令 pip install 模块名 (如果你觉得安装速度比较慢, 你可以切换国内镜像源)
既然是爬虫,那我们不得分析一下爬虫的基本思路。
数据来源分析
我们不管是爬什么,都要先找到数据来源对波。有来源才有下一步的行动。
代码流程步骤
有了来源目标之后,再请求获取数据,解析数据,最后创建文件夹保存到Excel表格。当然,如果想爬更多的,肯定就得实现自动翻页
所有代码
代码都在这,大家可以去试试,不限正规足浴,其实想爬啥都行。
import requests
import pprint
import re
import csv
import time
f = open('按摩data.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
'店铺名称',
'人均消费',
'店铺评分',
'评论人数',
'所在商圈',
'店铺类型',
'店铺地址',
'联系方式',
'营业时间',
'详情页',
])
csv_writer.writeheader()
def get_shop_info(html_url):
headers = {
'Cookie': '_lxsdk_cuid=1742973e754c8-0755662a43e0a2-3962420d-1fa400-1742973e754c8; iuuid=1F1D4BFAA0B9CA777B0EC2B899C43AD6C5C9CDB370B86A51322AB71211B94277; cityname=%E9%95%BF%E6%B2%99; _lxsdk=1F1D4BFAA0B9CA777B0EC2B899C43AD6C5C9CDB370B86A51322AB71211B94277; _hc.v=f4f02748-8eb7-1ea7-385c-5899047aa1c1.1618907157; __mta=251035321.1598423295952.1598423295952.1621410161604.2; uuid=05f4abe326934bf19027.1634911815.1.0.0; rvct=1%2C70%2C30; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; mtcdn=K; lt=knaBbvVTfN50cupoV5b87GJMXzkAAAAAAw8AAELrweWvhGhrM0fw6oTkLe5c6DGXJ6PCtxfyHgUPl3k-SVVR-Vs0LjzrGfewJhX8-g; u=266252179; n=qSP946594369; token2=knaBbvVTfN50cupoV5b87GJMXzkAAAAAAw8AAELrweWvhGhrM0fw6oTkLe5c6DGXJ6PCtxfyHgUPl3k-SVVR-Vs0LjzrGfewJhX8-g; unc=qSP946594369; firstTime=1634974011563; ci=70; _lxsdk_s=17cac0b849b-b3e-dac-85e%7C%7C10',
'Host': 'www.meituan.com',
'Referer': 'https://bj.meituan.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
}
response = requests.get(url=html_url, headers=headers)
# print(response.text)
phone = re.findall('"phone":"(.*?)"', response.text)[0]
openTime = re.findall('"openTime":"(.*?)"', response.text)[0].replace('\\n', '')
address = re.findall('"address":"(.*?)"', response.text)[0]
shop_info = [address, phone, openTime]
# print(shop_info)
return shop_info
# def get_shop_info(html_url):
# headers_1 = {
# 'Cookie': '_lxsdk_cuid=1742973e754c8-0755662a43e0a2-3962420d-1fa400-1742973e754c8; iuuid=1F1D4BFAA0B9CA777B0EC2B899C43AD6C5C9CDB370B86A51322AB71211B94277; cityname=%E9%95%BF%E6%B2%99; _lxsdk=1F1D4BFAA0B9CA777B0EC2B899C43AD6C5C9CDB370B86A51322AB71211B94277; _hc.v=f4f02748-8eb7-1ea7-385c-5899047aa1c1.1618907157; uuid=96d0bfc90dfc441b81fb.1630669508.1.0.0; ci=30; rvct=30; mtcdn=K; lt=1vyIlUgnzqEfhjpxic8Whf_WGfwAAAAAbg4AAOEUVbolB83IgwxL1wwOGfvIpVZsnHpwF6bGZZ5yT_SL4V8GRr4_WKkQ4s2AcF6Tmg; u=266252179; n=qSP946594369; token2=1vyIlUgnzqEfhjpxic8Whf_WGfwAAAAAbg4AAOEUVbolB83IgwxL1wwOGfvIpVZsnHpwF6bGZZ5yT_SL4V8GRr4_WKkQ4s2AcF6Tmg; firstTime=1630669549381; unc=qSP946594369; _lxsdk_s=17bab7a163a-27-d8f-8fd%7C%7C113',
# # 'Referer': 'https://sz.meituan.com/',
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
# }
# response_1 = requests.get(url=html_url, headers=headers_1)
# html_data = re.findall('"address":"(.*?)","phone":"(\d+)"', response_1.text)[0]
# return html_data
# get_shop_info('https://www.meituan.com/meishi/193587069/')
for page in range(0, 1537, 32):
time.sleep(2)
url = 'https://apimobile.meituan.com/group/v4/poi/pcsearch/30'
data = {
'uuid': '05f4abe326934bf19027.1634911815.1.0.0',
'userid': '266252179',
'limit': '32',
'offset': page,
'cateId': '-1',
'q': '按摩',
'token': 'knaBbvVTfN50cupoV5b87GJMXzkAAAAAAw8AAELrweWvhGhrM0fw6oTkLe5c6DGXJ6PCtxfyHgUPl3k-SVVR-Vs0LjzrGfewJhX8-g'
}
headers = {
'Referer': 'https://sz.meituan.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url=url, params=data, headers=headers)
result = response.json()['data']['searchResult']
for index in result:
shop_id = index['id']
index_url = f'https://www.meituan.com/meishi/{shop_id}/'
shop_info = get_shop_info(index_url)
dit = {
'店铺名称': index['title'],
'人均消费': index['avgprice'],
'店铺评分': index['avgscore'],
'评论人数': index['comments'],
'所在商圈': index['areaname'],
'店铺类型': index['backCateName'],
'店铺地址': shop_info[0],
'联系方式': shop_info[1],
'营业时间': shop_info[2],
'详情页': index_url,
}
csv_writer.writerow(dit)
print(dit)
还可以实现数据分析啥的,我就不往下写了,前两天的一篇,有实现数据分析的,同一个平台的。
兄弟们,看完记得点个赞三连啥的,这样我更新就更快了,我就喜欢快~
给你们奖励一个女朋友!
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/fei347795790/article/details/120962252
内容来源于网络,如有侵权,请联系作者删除!