zaheer
zaheer

Reputation: 143

Scraping SVG tags from website using Beautiful Soup

I am trying to scrape SVG tags from a website. The issue is when I manually copy the SVG tag and save it (for example image.svg) it perfect., but when scraping and save as .svg file the image is broken and error.

here is the code:

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')

  

Upvotes: 1

Views: 2433

Answers (1)

Epsi95
Epsi95

Reputation: 9047

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))

Upvotes: 1

Related Questions