1

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') 
13
  • can you provide full code and website url? Commented Jan 26, 2021 at 13:59
  • this is website hudl.com it requires login Commented Jan 26, 2021 at 14:11
  • didn't find any class named drawing-area in the page content. Are you targetting the correct class name? Commented Jan 26, 2021 at 14:17
  • sorry, this is the correct url to page hudl.com/playtools/playbook/manage/27073/offense/play/… however it requires login, you can replace class name with this for home page "mobile-toggle", thanks Commented Jan 26, 2021 at 14:18
  • 1
    can you check the code, it is working my side Commented Jan 26, 2021 at 14:34

1 Answer 1

1
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)) 
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, this code run perfectly the issue is if i open the image_0.svg its broken image.
that is strange, it is working fine at my side
i will check downloading a new image viewer. thanks for the help
you can use google chrome to view it. Right click and open with chrome or internet explorer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.