简体   繁体   中英

How to extract html using beautifulsoup?

The HTML source was

html = """
<td>
 <a href="/urlM5CLw" target="_blank">
  <img alt="I" height="132" src="VZhAy" width="132"/>
 </a>
 <br/>
 <cite title="mac-os-x-lion-icon-pack.en.softonic.com">
  mac-os-x-lion-icon-pac...
 </cite>
 <br/>
 <b>
  Mac
 </b>
 OS X Lion Icon Pack's
 <br/>
 535 × 535 - 135k - png
</td>"""

My python code

soup = BeautifulSoup(html)
text = soup.find('td').renderContents()

By these code I can get string like

<a href="/urlM5CLw" target="_blank"><img alt="I" height="132" src="VZhAy" width="132"/></a><br/><cite title="mac-os-x-lion-icon-pack.en.softonic.com">mac-os-x-lion-icon-pac...</cite><br/><b>Mac</b> OS X Lion Icon Pack's<br/>535 × 535 - 135k - png

But I don't want <a>....</a> , I just need:

<br/><cite title="mac-os-x-lion-icon-pack.en.softonic.com">mac-os-x-lion-icon-pac...</cite><br/><b>Mac</b> OS X Lion Icon Pack's<br/>535 × 535 - 135k - png

Try removing the <a> tag and then fetch what you were trying to.

>>> soup.find('a').extract()
>>> text = soup.find('td').renderContents()
>>> text
'<br/><cite title="mac-os-x-lion-icon-pack.en.softonic.com">mac-os-x-lion-icon-pac...</cite><br/><b>Mac</b> OS X Lion Icon Pack's<br/>535 \xd7 535 - 135k - png'

You can use the Tag.decompose() method to remove the a tag and completely destroy his contents also you may need to decode() your byte string and replace all \\n occurence by '' .

soup = BeautifulSoup(html, 'lxml')
soup.a.decompose()
print(soup.td.renderContents().decode().replace('\n', ''))

yields:

<br/><cite title="mac-os-x-lion-icon-pack.en.softonic.com">  mac-os-x-lion-icon-pac... </cite><br/><b>  Mac </b> OS X Lion Icon Pack's <br/> 535 × 535 - 135k - png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM