Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

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

  
question from:https://stackoverflow.com/questions/65902700/scraping-svg-tags-from-website-using-beautiful-soup

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...