我制作了一个从两个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