如何从这个程序返回多个值到 API 主机服务器
Posted
技术标签:
【中文标题】如何从这个程序返回多个值到 API 主机服务器【英文标题】:How to return multiple values from this program to API host server 【发布时间】:2021-11-29 12:05:32 【问题描述】:这是一个从实习站点抓取数据然后将其转换为将数据以 JSON 格式发布到本地服务器的 rest API 的程序
注意:- 这是整个程序的一部分,它可以独立运行。
import scrapped_intershala
from flask import Flask, jsonify
import json
import requests
import webbrowser
from bs4 import BeautifulSoup
start_link='https://internshala.com'
html_text = requests.get('https://internshala.com/internships/game%20development-internship').text #for game development
soup= BeautifulSoup (html_text,'lxml')
app = Flask(__name__)
@app.route('/')
def run():
company_name= soup.find_all('div', class_="heading_4_5 profile")
stipend = soup.find_all(('span'), class_="stipend")
location = soup.find_all(('a'), class_="location_link")
start_date = soup.find_all(('div'), class_="item_body", id="start-date-first")
link_to = soup.find_all(('div'),class_="heading_4_5 profile")
for name, sal, loc, Sdate, LK in zip(company_name, stipend, location, start_date, link_to): #zip takes iterable or containers and returns a single iterator object, having mapped values from all the containers.
#name below are only here as these need formatting
nm = name.text.replace('\n','') #name of company
dt = Sdate.text.replace('\n','').replace('Immediately','').strip() #date of joining as intern
#name above are only here as these need formatting
answer=
'Company name' : nm,
'Stipend' : sal.text,
'Location': loc.text,
'Date of joining as intern' : dt,
'For more information link for internship': start_link+LK.a['href']
queries_json = json.dumps(answer)
return queries_json
#print(f'''Company name: nm\n\nStipend: sal.text\n\nLocation: loc.text\n\nDate of joining as intern: dt\n\nFor more information link for internship: start_link+LK.a['href']\n\n\n\n\n\n''')
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=7000) #this command may chance for every machine / / here host 0.0.0.0 is used as this machine uses a private IP
本程序只向本地服务器返回一个值,而不是向服务器返回所有值
【问题讨论】:
【参考方案1】:python 中的return
语句会中断执行,因此如果在 for 循环中使用它会中断循环。
你应该做的是使用 foror 循环创建一个列表对象,然后将其返回:
all_answers = []
for item in ...:
answer = ...
all_answers.append(answer)
return json.dumps(all_answers)
【讨论】:
以上是关于如何从这个程序返回多个值到 API 主机服务器的主要内容,如果未能解决你的问题,请参考以下文章