I'm not just eating some meat, it should be fine - crawling the data of barbecue restaurants in a city (with complete Python crawler code)

A bit of bullshit written on the front:

For carnivores, eating meat can be overwhelming! Especially for barbecued meat, watching the pieces of meat slowly become cooked and listening to the sound of "zizi" on the grill, this kind of anticipation cannot be brought by any other food. If dessert is "happy at first sight", then meat is "never tire of it."
Please add image description
In order to benefit the "barbecue control", today I will use Python to crawl the data of a city's barbecue restaurants, and choose the most suitable one!

Ready to work

environment

  • python 3.6
  • pycharm
  • requests >>> send requests pip install requests
  • csv >>> save data

Understand the most basic ideas of reptiles

1. Data source analysis

  1. Determine what content we crawl?
    Crawl store data
  2. Find out where these things come
    from
  3. send request, send request for found packets
  4. Get data, based on the response data returned by the server to you
  5. Parse the data, extract the content data we want
  6. save data, save to csv file
  7. Multi-page crawling, changes according to url address parameters

code implementation process

  1. send request
url = 'https://apimobile.某tuan.com/group/v4/poi/pcsearch/70'
data = {
    'uuid': '6e481fe03995425389b9.1630752137.1.0.0',
    'userid': '266252179',
    'limit': '32',
    'offset': 32,
    'cateId': '-1',
    'q': '烤肉',
    'token': '4MJy5kaiY_0MoirG34NJTcVUbz0AAAAAkQ4AAF4NOv8TNNdNqymsxWRtJVUW4NjQFW35_twZkd49gZqFzL1IOHxnL0s4hB03zfr3Pg',
}
# 请求头 都是可以从开发者工具里面直接复制粘贴
# ser-Agent: 浏览器的基本信息
headers = {
    'Referer': 'https://chs.某tuan.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'
}
# 发送请求
response = requests.get(url=url, params=data, headers=headers)

200 indicates that the request was successful and the status code is 403. You do not have access rights

2. Get data

print(response.json())

3. Parse the data

result = response.json()['data']['searchResult']
# [] 列表 把里面每个元素都提取出来 for循环遍历
for index in result:
    # pprint.pprint(index)
    # f'{}' 字符串格式化
    index_url = f'https://www.某tuan.com/meishi/{index["id"]}/'
    # ctrl + D
    dit = {
        '店铺名称': index['title'],
        '店铺评分': index['avgscore'],
        '评论数量': index['comments'],
        '人均消费': index['avgprice'],
        '所在商圈': index['areaname'],
        '店铺类型': index['backCateName'],
        '详情页': index_url,
    }
    csv_writer.writerow(dit)
    print(dit)

4. Save data

f = open('烤肉数据.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
    '店铺名称',
    '店铺评分',
    '评论数量',
    '人均消费',
    '所在商圈',
    '店铺类型',
    '详情页',
])
csv_writer.writeheader() # 写入表头

5. Turn the page

for page in range(0, 1025, 32):
    url = 'https://apimobile.某tuan.com/group/v4/poi/pcsearch/70'
    data = {
        'uuid': '6e481fe03995425389b9.1630752137.1.0.0',
        'userid': '266252179',
        'limit': '32',
        'offset': page,
        'cateId': '-1',
        'q': '烤肉',
        'token': '4MJy5kaiY_0MoirG34NJTcVUbz0AAAAAkQ4AAF4NOv8TNNdNqymsxWRtJVUW4NjQFW35_twZkd49gZqFzL1IOHxnL0s4hB03zfr3Pg',
    }

Run the code to get the data

Please add image description
Please add image description
More information can be added to the Q group click here

full code

f = open('烤肉数据1.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
    '店铺名称',
    '店铺评分',
    '评论数量',
    '人均消费',
    '所在商圈',
    '店铺类型',
    '详情页',
])
csv_writer.writeheader() # 写入表头

for page in range(0, 1025, 32):
    url = 'https://apimobile.某tuan.com/group/v4/poi/pcsearch/70'
    data = {
        'uuid': '6e481fe03995425389b9.1630752137.1.0.0',
        'userid': '266252179',
        'limit': '32',
        'offset': page,
        'cateId': '-1',
        'q': '烤肉',
        'token': '4MJy5kaiY_0MoirG34NJTcVUbz0AAAAAkQ4AAF4NOv8TNNdNqymsxWRtJVUW4NjQFW35_twZkd49gZqFzL1IOHxnL0s4hB03zfr3Pg',
    }
    headers = {
        'Referer': 'https://chs.某tuan.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'
    }
    # 发送请求
    response = requests.get(url=url, params=data, headers=headers)
    # 200 表示请求成功 状态码 403 你没有访问权限

    result = response.json()['data']['searchResult']
    # [] 列表 把里面每个元素都提取出来 for循环遍历
    for index in result:
        # pprint.pprint(index)
        # f'{}' 字符串格式化
        index_url = f'https://www.meituan.com/meishi/{index["id"]}/'
        # ctrl + D
        dit = {
            '店铺名称': index['title'],
            '店铺评分': index['avgscore'],
            '评论数量': index['comments'],
            '人均消费': index['avgprice'],
            '所在商圈': index['areaname'],
            '店铺类型': index['backCateName'],
            '详情页': index_url,
        }
        csv_writer.writerow(dit)
        print(dit)

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/124131681