python爬虫数据写入文件,及下载功能实现

from django.http import HttpRequest, HttpResponse, FileResponse
from urllib.request import urlopen, Request
from urllib import parse
from lxml import etree
import ssl


#爬虫爬取写入文件
def worm(request: HttpRequest):
    urls = '/user/worm?urls=https://www.baidu.com/s'
    data = {
        'wd': '娄宇'
    }
    ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
    requestWorm = Request("{}?{}".format(urls, parse.urlencode(data)))
    requestWorm.add_header('User-Agent', ua)
    context = ssl._create_unverified_context()

    with urlopen(requestWorm, context=context) as response:
        returnValue = response.read()
        html = etree.HTML(returnValue)
        returnValue = html.xpath("//h3[@class='t c-gap-bottom-small']//text()")
        
        with open('./data.txt', 'ab+') as file:
            for i in returnValue:
                s=bytes(i,encoding='utf-8')
                file.write(s)
        return HttpResponse(returnValue)

 #下载功能
def downLoad(request: HttpRequest):
    with open('./data.txt') as f:
        file = f.read()
        response = FileResponse(file)
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition']='attachment;filename="models.py"'
        return response
    

 

你可能感兴趣的:(后端)