我正在尝试从网站抓取SVG标签。问题是当我手动复制SVG标记并保存它(例如image.svg
)时,它是完美的。,但当抓取并保存为.svg
文件时,图像被破坏和错误。
from urllib.request import Request
from bs4 import BeautifulSoup as soup
image_url = 'https://www.hudl.com/'
request = Request(image_url, headers={'User-Agent': 'Mozilla/5.0'})
client = urlRequest(request)
# time.sleep(1)
data = client.read()
time.sleep(2)
# image
data_soup = soup(data, 'html.parser')
image_ = data_soup.find('div', {'class': 'mobile-toggle'})
image_ = image_.find('svg')
url = 'https://www.hudl.com/en_gb/'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
image_ = soup.find_all('div', {'class': 'mobile-toggle'})
image_ = image_.find('svg')
image_ = [i.find('svg') for i in image_]
for index, i in enumerate(image_):
with open(f'image_{index}.svg', 'w') as f:
f.write(str(i))