【发布时间】:2022-10-14 16:11:13
【问题描述】:
import requests
from bs4 import BeautifulSoup
import wget # Downloads files from url
page = requests.get("https://en.wikipedia.org/wiki/Gallery_of_sovereign_state_flags")
soup = BeautifulSoup(page.content, 'html.parser')
for flag in soup.find_all('a', attrs={'class': "image"}):
src = flag.contents[0]['src']
src = src.replace("thumb/", "")
src = "https:" + src
sep = '.svg'
fixed_src = src.split(sep, 1)[0] + ".svg"
print(fixed_src)
for country in data["Country"]: # A column containing country names
if country in fixed_src:
wget.download(fixed_src, f'flags/{country}.svg')
它适用于大多数生成的 url,但一旦到达“澳大利亚”,它就会返回 urllib.error.HTTPError: HTTP Error 404: Not Found。但是当我按下链接时,它会将我重定向到它并找到它。
我尝试放置一个 if 语句来忽略澳大利亚,但很少有其他 url 返回相同的错误。
有任何想法吗?
【问题讨论】:
标签: python beautifulsoup python-requests-html