python - 如何从这个程序返回多个值到 API 主机服务器
问题描述
这是一个从实习站点抓取数据然后将其转换为将数据作为 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
该程序只向本地服务器返回一个值,而不是向服务器返回所有值
解决方案
return
python中的语句会中断执行,所以如果你在for循环中使用它会中断循环。
您应该做的是使用 foror 循环创建一个列表对象,然后将其返回:
all_answers = []
for item in ...:
answer = {...}
all_answers.append(answer)
return json.dumps(all_answers)
推荐阅读
- c# - UIElements 的 WPF 自动化
- batch-file - 如何通过 bat 文件向 exe 提供输入
- windows - 为系统调用挂钩 Windows 内核调度程序
- javascript - 意外令牌:名称(stateCheck)(行:23,列:4)
- java - 如何在 Android Java 中模拟触摸事件
- javascript - why won't jquery datatable respond to click?
- oracle - 我可以在不安装 Oracle 数据库的情况下使用 Sqlplus 即时客户端吗?
- c++ - C ++将变量的地址分配给另一个变量
- firebase - Dart Firebase,无法使用 orderByValue 对列表进行排序
- python-3.x - python sklearn accuracy_score 名称未定义