Я сделал программу очистки данных из двух URL: {"url": "https://forecast.weather.gov/MapClick.php?lat=37.7772&lon=-122.4168#.XaA9cEZKiUk"," svgurl ":" https://www.amcharts.com/wp-content/themes/amcharts4/css/img/icons/weather/animated/cloudy.svg"} первый только информационный текст и второйчтобы сохранить данные SVG, поэтому я хочу выполнить две функции одновременно, не используя поток, используя вместо этого asyncio
Моя цель - преобразовать синхронный код Python в асинхронный код, это то, что я сделал для этого:
from django.shortcuts import render
import requests
import json
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.http import JsonResponse
from django.core.serializers import serialize
import urllib.parse as urlparse
import base64
from cairosvg import svg2png
from rest_framework.decorators import api_view
requests.packages.urllib3.disable_warnings()
# Create your views here.
from bs4 import BeautifulSoup
import asyncio
@api_view(["POST"])
def Post(request):
global srcsnapcodeimg,soup,tonight,sourcesvg
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
url = body['url']
urlsvg=body['svgurl']
source = requests.get(url).text
sourcesvg=requests.get(urlsvg).text
soup = BeautifulSoup(source, "html.parser")
seven_day = soup.find(id="seven-day-forecast")
forecast_items = seven_day.find_all(class_="tombstone-container")
tonight = forecast_items[0]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
tasks = [
asyncio.ensure_future(Getvalues()),
asyncio.ensure_future(Get_bin_img())]
loop.run_until_complete(asyncio.wait(tasks))
print(Get_bin_img())
Getvalues_dict = json.loads(asyncio.wait(Getvalues()))
data={"period":Getvalues_dict['period'] ,"short_desc":Getvalues_dict['short_desc'],"temp":Getvalues_dict['temp']}
#print(data)
return JsonResponse(data)
@asyncio.coroutine
def Getvalues():
period = tonight.find(class_="period-name").get_text()
short_desc = tonight.find(class_="short-desc").get_text()
temp = tonight.find(class_="temp").get_text()
datanameboxandprice={"period":period,"short_desc":short_desc,"temp":temp}
return json.dumps(datanameboxandprice)
@asyncio.coroutine
def Get_bin_img():
svg2png(bytestring=sourcesvg,write_to='output.png')
with open("output.png", "rb") as imageFile:
base64code = base64.b64encode(imageFile.read())
return base64code
у меня есть эта ошибка в моем почтальоне: когда я отправляю почтовый запрос:
объект JSON должен быть str, bytes или bytearray, а не 'generator'
синхронныйкод работает нормально:
from django.shortcuts import render
import requests
import json
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.http import JsonResponse
from django.core.serializers import serialize
import urllib.parse as urlparse
import base64
from cairosvg import svg2png
from rest_framework.decorators import api_view
requests.packages.urllib3.disable_warnings()
# Create your views here.
from bs4 import BeautifulSoup
@api_view(["POST"])
def Post(request):
global srcsnapcodeimg,soup,tonight,sourcesvg
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
url = body['url']
urlsvg=body['svgurl']
source = requests.get(url).text
sourcesvg=requests.get(urlsvg).text
soup = BeautifulSoup(source, "html.parser")
seven_day = soup.find(id="seven-day-forecast")
forecast_items = seven_day.find_all(class_="tombstone-container")
tonight = forecast_items[0]
print(Get_bin_img())
Getvalues_dict = json.loads(Getvalues())
data={"period":Getvalues_dict['period'] ,"short_desc":Getvalues_dict['short_desc'],"temp":Getvalues_dict['temp']}
#print(data)
return JsonResponse(data)
def Getvalues():
period = tonight.find(class_="period-name").get_text()
short_desc = tonight.find(class_="short-desc").get_text()
temp = tonight.find(class_="temp").get_text()
#PrevClosevalue=soup.find('div',class_='value__b93f12ea').text.strip()
datanameboxandprice={"period":period,"short_desc":short_desc,"temp":temp}
return json.dumps(datanameboxandprice)
def Get_bin_img():
svg2png(bytestring=sourcesvg,write_to='output.png')
with open("output.png", "rb") as imageFile:
base64code = base64.b64encode(imageFile.read())
return base64code