Kaj Kam
Kaj Kam

Reputation: 1

Avoid space in class value in scraping in Python

I was trying to crawl some data from a website. Here I have found space characters in class value in the website like:

<div class="Col No 3">
    <div class="row No 4"> 
    </div>
</div>

In this case I used:

files = bsObj.find_all("div", {"class":"row No 4"})
for file in files:
    print (file.get_text())

I have also used (.) dot characters in value in the place of spaces ("row.No.4").

files = bsObj.find_all("div", {"class":"row.No.4"})
for file in files:
    print (file.get_text())

It still gives an error.

Upvotes: 0

Views: 98

Answers (1)

R.yan
R.yan

Reputation: 2372

Based on your syntax and content, I assume you are using BeautifulSoup package and want to find files by multiple classes.

Using .find_all():

files = bsObj.find_all("div", {"class":['row','No','4']})
for file in files:
    print (file.get_text())

On the other hand, If you go for selectors then you need to use dot . to join multiple classes:

Using .select():

files = bsObj.select("div.row.No.4")
for file in files:
    print (file.get_text())

Upvotes: 1

Related Questions