无法使用请求模块无法抓取页面,该模块不适用于BeautifulSoup的flask API

时间:2020-07-03 10:02:57

标签: python flask beautifulsoup python-requests

在这里我正在使用Flask API模块

@app.route('/url', methods=['GET'])
def api_url():
    if 'web_url' in request.args:
        web_url = str(request.args['web_url'])

    html = requests.get(web_url).text
    soup = BeautifulSoup(html, 'lxml')
    web_page = soup.get_text().strip()
    
    return (web_page)

当我给

http://127.0.0.1:5000/url?url=https://stackoverflow.com 它不是在抓取网页,而是与API完美结合

像例子

html = requests.get(web_url).text
soup = BeautifulSoup(html, 'lxml')
web_page = soup.get_text().strip()
print(web_page)

在这里我就做
import request as requests会不会有问题??与request.args 我只需要抓取的网页html代码作为我正在搜索的输出 就像我们在google crome view page source上那样尝试获取输出

任何建议

1 个答案:

答案 0 :(得分:0)

您应该从烧瓶导入请求 并发送您的请求,需要导入请求

import flask  # to get the args and run the http server
import requests # to send the get request
@app.route('/url', methods=['GET'])
def api_url():
    if 'web_url' in flask.request.args:
        web_url = str(flask.request.args['web_url'])

     html = requests.get(web_url).text
     soup = BeautifulSoup(html, 'lxml')
     web_page = soup.get_text().strip()

     return (web_page)