1

I want to catch a tag from the html code below.

<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">

now I catch "pre" tag by pres = soup.find_all("pre").
then I want to catch the tag whose class's name is source.
but when I type like :

pre = soup.find(class = "source")

the "class" attribute will clash with python keyword "class".
how can I catch the tag whose class = "source"?

Aditi
  • 820
  • 11
  • 27
Aaron Hao
  • 59
  • 8

2 Answers2

3

It's because the word class is a keyword, that it cannot be used as an argument name. For that reason, you've to add a trailing underscore:

pre = soup.find(class_='source')

Or, pass a dictionary to the attrs parameter:

pre = soup.find(attrs={'class' : 'source'})
cs95
  • 379,657
  • 97
  • 704
  • 746
2

Try this:

pre = soup.find_all("pre", {'class':'source'})

OR

pre = soup.find("pre", {'class':'source'})

Note: In your example your code crashes beacause class is a keyword, it cannot be used as an argument name.

Hope this will help you! Thankyou! :)

Abdullah Ahmed Ghaznavi
  • 1,978
  • 3
  • 17
  • 27