How to insert an attribute using BeautifulSoup?

How to insert an attribute using BeautifulSoup?

For example, insert the border = "1" attribute as the tag attribute.

EDIT:

I answered my question (for a specific class of the table even):

inTopic = urllib2.urlopen ("file: /// C: /test/test.html") content = BeautifulSoup (inTopic)

tlist = content.findAll ('table', "myTableClass") for tbl in tlist: tbl ['border'] = "1" print tbl.attrs

+3
source share
1 answer

What about:

inTopic = urllib2.urlopen('http://stackoverflow.com/questions/4951331/how-do-i-insert-an-attribute-using-beautifulsoup')
content = BeautifulSoup.BeautifulSoup(inTopic)
tlist = content.findAll('table')
for tbl in tlist:
    tbl.attrs.append(('border', 1))

Do not forget to try lxml.html, quickly and analyze.

+4
source

Source: https://habr.com/ru/post/1791085/


All Articles