python爬虫使用flask制作API

 1.编写爬虫


import requests


class TestFlaskApi:
    def __init__(self):
        self.url = 'http://127.0.0.1:1688/'   # flask设置的本地地址和端口
        # 此处的self.headers可以省略,本地不用设置
        self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}
    
    def parse(self):
        # data为请求flask中定义的参数,一定得对应上
        data = {'funItemMenuId': 115990101,
                'position': 'Gis'}
        response = requests.get(self.url=url, self.headers=headers, params=data)
        if response.status_code == 200:
            return response.text
        else:
            print('请检查网站情况')


if __name__ == '__main__':
    t = TestFlaskApi()
    t.parse()

  2.编写flask API


# coding:utf8
import requests
import flask
from flask import request

server = flask.Flask(__name__)

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
                         ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}


@server.route('/', methods=['get', 'post'])
def reg():
    funItemMenuId = request.values.get('funItemMenuId')  # 设置参数
    position = request.values.get('position')
    # 自己定义url,这里隐藏了部分url,但功能是一样的
    url = 'funItemMenuId={0}&position={1}.format(funItemMenuId, position)
    r = requests.get(url, headers=headers)
    return r.text

if __name__ == '__main__':
    server.run(port=1688, debug=True, host='127.0.0.1')

3.得到数据

python爬虫使用flask制作API_第1张图片

你可能感兴趣的:(爬虫)