'Removing newlines inside <a> tag
I am quite new to python and also BeautifulSoup. Recently, I tried to get extract every <a> tag from some HTML in a local file. Part of my code looks like this:
with open(dir_path,encoding="utf-8_sig") as html_file:
soup =BeautifulSoup(html_file,'html.parser')
tag = soup.find('a')
print(tag)
The output is look like this
<a class="cmp-image__link" data-cmp-hook-image="link" href="/">
<img alt="logo" class="cmp-image__image" data-cmp-hook-image="image" itemprop="contentUrl" src="//imagesource"/>
</a>
What I want to get is a string without newline inside the block of <a> tag
<a class="cmp-image__link" data-cmp-hook-image="link" href="/"><img alt="logo" class="cmp-image__image" data-cmp-hook-image="image" itemprop="contentUrl" src="//imagesource"/></a>
I tried using .strip() and .replace(), but it didnt work. Please help!
Update answer
This actually work well in my case !!! from @BrokenBenchmark
tag = '''<a class="cmp-image__link" data-cmp-hook-image="link" href="/">
<img alt="logo" class="cmp-image__image" data-cmp-hook-image="image" itemprop="contentUrl" src="//imagesource"/>
</a>'''
result = tag.replace('\n', '')
print(result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
