【问题标题】:can't scrape the page using request module not working with flask API with BeautifulSoup无法使用请求模块刮取页面,无法使用带有 BeautifulSoup 的烧瓶 API
【发布时间】:2023-12-29 14:27:01
【问题描述】:

这里我用的是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://*.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 上做的那样,我试图获得输出

任何建议

【问题讨论】:

    标签: python flask beautifulsoup python-requests


    【解决方案1】:

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

    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)
    

    【讨论】:

      最近更新 更多