美团爬虫总结

寄了

难点

反扒:uuid token变动 ---请求头参数的封装

动态网站 json

模拟登录:cookies

import requests

import json
import pprint #输出更好看

base_url = 'https://bj.meituan.com/ptapi/recommends?limit=10?'



uuid = "" # 你的uuid,登录后在开发者模式获取

userid = "" # 你的userid,登录后在开发者模式获取



key = '大盘鸡' 



page = 1

\# 设置请求参数

parameters = {

  'uuid': uuid, # 你的uuid,登录后在开发者模式获取

  'userid': userid, # 你的userid,登录后在开发者模式获取

  'limit':32, # 每页的 店铺信息数

  'offset':32*(page - 1), # 当前 偏移量,第1页为0,第2页为 (2-1)*limit

  'cateId':-1, #

  'q': key, # 搜索的关键字

  }

\# 设置请求头

header = {
#增加防盗链 ,cookies,把请求头的都加一遍试一试

# "Accept-Encoding": "Gzip",  # 使用gzip压缩传输数据让访问更快

  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",

  }



responce = requests.get(base_url, headers = header, params=parameters)
print(responce)#测试通不通



text = responce.text   #得出字符串数据
text = responce.json    #获取字典数据

\# 由于是json格式的字符串,用json.load()方法格式化

print(text)

扫描二维码关注公众号,回复: 14727760 查看本文章

401错误场景 响应器拦截

\1. 用户未登录,代码报401,应该回到登录页

\2. 登录用户的token过期 :

. 就是登录成功了以后,后端会返回一个token值,这个值在后续请求时带上(就像是开门钥匙),

但是,这个值一般会有有效期(具体是多长,是由后端决定)token过期是为了安全起见

每次及时跟新tonken????? 我的背封印了???

(3条消息) 401错误 & 解决方法:响应拦截器_不秃头的xx的博客-CSDN博客

分析网页

开发者模式

解析数据

字典遍历

searchResult=response.json()['data']['soarchResult']

for index in searchResult:

    href=f'https://www.meituan.com/xiuxianyule/{index["id"]}/'#

    dit=[

    '店铺名':index['title']

    '评分':index['avgscore']

    '商国':index['areaname']

    '人均消费':index['avgprice']

    '店铺类型':index['backCateName']

    '详论量':index['comments']

    '详情项':href

    ]

详情页处理

格式化

文件保存--字典写入

全部代码

import requests
import json
import pprint
import csv
​
base_url = 'https://apimobile.meituan.com/group/v4/poi/pcsearch/70'
#白金汉https://www.meituan.com/ptapi/poi/getcomment?id=1496329&offset=10&pageSize=10&mode=0&starRange=&userId=&sortType=1&enableGuard=truehttps://www.meituan.com/ptapi/poi/getcomment?id=1496329&offset=10&pageSize=10&mode=0&starRange=&userId=&sortType=1&enableGuard=true
#自然公园https://www.meituan.com/ptapi/poi/getcomment?id=1235523&offset=0&pageSize=10&mode=0&sortType=1&enableGuard=true
​
​
​
#w文件写入
f=open('美团爬虫尝试.csv',mode='a',encoding='utf-8',newline='')
​
# csv.DictWriter(f,filednames=[])  #字典写入
​
csv_writer=csv.DictWriter(f,fieldnames=[
    '店铺名',
    '评分',
    '商国',
    '人均消费',
    '店铺类型',
    '详论量',
    '详情项',
])
csv_writer.writeheader()#写入表头
​
#自己的密钥
uuid = "cf32f91085b44b87955c.1679756420.1.0.0" # 你的uuid,登录后在开发者模式获取
userid = "3682664218" # 你的userid,登录后在开发者模式获取
#token=''
​
#key = '大盘鸡' 
​
page = 1
​
# 设置请求参数
​
parameters = {
    'uuid': uuid, # 你的uuid,登录后在开发者模式获取
    'userid': userid, # 你的userid,登录后在开发者模式获取
    'limit':32, # 每页的 店铺信息数
    'offset':32*(page - 1), # 当前 偏移量,第1页为0,第2页为 (2-1)*limit
    'cateId':-1, #
​
    # 'q': key, # 搜索的关键字
​
•    }
​
# 设置请求头
​
header = {
    "Referer": "https://www.meituan.com/zhoubianyou/1235523/",
    "Accept-Encoding": "Gzip",  # 使用gzip压缩传输数据让访问更快
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
    }
​
responce = requests.get(base_url, headers = header, params=parameters)
print(responce)
text = responce.text
​
# 由于是json格式的字符串,用json.load()方法格式化
​
# print(text)
​
​
​
​
#最后一部
#数据保存
csv_writer.writerow(dit)
print(dit)
​
​
​
​
#先解析网页的结构
searchResult=response.json()['data']['soarchResult']
for index in searchResult:
    href=f'https://www.meituan.com/xiuxianyule/{index["id"]}/'#
    dit=[
    '店铺名':index['title']
    '评分':index['avgscore']
    '商国':index['areaname']
    '人均消费':index['avgprice']
    '店铺类型':index[backCateName']
    '详论量',
    '详情项':href
    ]

猜你喜欢

转载自blog.csdn.net/m0_69379600/article/details/129834196