import requests
import gevent
from gevent import monkey
monkey.patch_all()
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
}
def download(url):
print("start:",url)
content = requests.get(url, headers=headers).content
gevent.sleep(1)
print(len(content), url)
pass
def main():
gevent.joinall([
gevent.spawn(download, "http://www.baidu.com"),
gevent.spawn(download, "http://www.bilibili.com"),
gevent.spawn(download, "http://www.163.com"),
gevent.spawn(download, "http://www.sina.com"),
])
if __name__ == "__main__":
main()