我正试图从美国人口普查局下载压缩文件(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/[文件链接]“
因此,当前您的域变量是错误的,它应该使用您的URL变量
目前,“with open”也没有缩进以处理for循环中的所有链接,因此在代码的当前状态下只下载最后一个链接
相关问题 更多 >
编程相关推荐