简体   繁体   中英

Python print scraped data with beautifulsoup without tags

<div class="number" title="Player number">1211</div>
<div class="shirt" title="sName">Ronaldo 1211</div>

I'm scraping a website. I've managed to print out the . Here is my code:

web = urllib2.urlopen("WEBSITE")
soupit = BeautifulSoup(web, 'html.parser')
scrapeme = soupit.findAll("div", { "class" : "number" })
print scrapeme

prints out :

<div class="id" title="Player number">1211</div>

I want it to print just the 1211. How can I do it?

任何 beautifulsoup 对象的get_ text()方法就是这样做的。

print(scrapeme.get_text())

Once you have your list of elements, scrapeme , you can loop through each element in the list and print it's text attribute using:

for element in scrapeme:
    print(element.text)

Since in your example the scrape only generates a list scrapeme containing one element, the output in this case will just be:

1211

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