简体   繁体   中英

Scraping data with bs4

I have this line of html:

<td title="Druid"><a href="/pvp/druid"><img src="/images/classes/druid.png" class="img-responsive center" alt="Druid"></a></td>

Using python and beautiful soup, I'd like to access the the "Druid" from <td title="Druid"> to be stored as a variable.

If you want to access the title of the td:

bs4.BeautifulSoup(data).find("td")["title"]

To get the attributes of a tag treat it just like a dictionary:

soup.find('td').get('title')

or

soup.find('td')['title']

Note: .get('title', 'some default value') allows you to provide a default value if the key is missing or if omitted is None , while ['title'] will raise a KeyError

Example

from bs4 import BeautifulSoup
html='''<td title="Druid"><a href="/pvp/druid"><img src="/images/classes/druid.png" class="img-responsive center" alt="Druid"></a></td>'''
soup = BeautifulSoup(html)

character = soup.find('td').get('title')

print(character)

Output

Druid

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