![](/img/trans.png)
[英]How to download multiple zip files using requests and extracting to a folder using Python
[英]Using python + requests to download multiple zip files from links
我正在嘗試從美國人口普查局 ( https://www2.census.gov/geo/tiger/TIGER2019/PLACE/ ) 下載壓縮文件。 到目前為止,我的代碼似乎可以工作,但下載的所有文件都是空的。 有人可以幫助填寫我所缺少的嗎?
from bs4 import BeautifulSoup as bs
import requests
import re
DOMAIN = "https://www2.census.gov/"
URL = "https://www2.census.gov/geo/tiger/TIGER2019/PLACE/"
def get_soup(URL):
return bs(requests.get(URL).text, 'html.parser')
for link in get_soup(URL).findAll("a", attrs={'href': re.compile(".zip")}):
file_link = link.get('href')
print(file_link)
with open(link.text, 'wb') as file:
response = requests.get(DOMAIN + file_link)
file.write(response.content)
看起來您使用了錯誤的鏈接。
查看網站后,我可以看到下載鏈接有以下URL:“https://www2.census.gov/geo/tiger/TIGER2019/PLACE/[file_link]”,
所以目前你的 DOMAIN 變量是錯誤的,它應該使用你的 URL 變量。
目前,“with open”也沒有縮進與 for 循環中的所有鏈接一起使用,因此只有最后一個鏈接下載到當前 state 您的代碼中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.