我想用python尽可能快地下载文件.这是我的代码
import pandas as pd
import requests
from requests_futures.sessions import FuturesSession
import os
import pathlib
from timeit import default_timer as timer
class AsyncDownloader:
"""Download files asynchronously"""
__urls = set()
__dest_path = None
__user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
__read_timeout = 60
__connection_timeout = 30
__download_count = 0 # unlimited
# http://www.browserscope.org/?category=network
__worker_count = 17 # No of threads to spawn
__chunk_size = 1024
__download_time = -1
__errors = []
# TODO Fetch only content of …Run Code Online (Sandbox Code Playgroud) python performance python-3.x python-requests requests-futures
我想知道如何在以下 get 调用中传递标头
headers = {
'User-Agent': 'Mozilla'
}
async def fetch(url, session):
async with session.get(url) as response:
resp = await response.read()
return resp
Run Code Online (Sandbox Code Playgroud)
我尝试了以下但没有得到任何回应。
headers = {
'User-Agent': 'Mozilla'
}
async def fetch(url, session):
async with session.get(url, headers=headers) as response:
resp = await response.read()
return resp
Run Code Online (Sandbox Code Playgroud)
目的是以异步模式调用不同的 url。需要知道是否还有其他替代方法,但无论如何,都需要传递标头才能获得正确的响应。