Search code examples
pythonwebscreen-scraping

Web scraping returns tags


I'm a complete noob to HTML. I had to look up "tags" and "class" before I wrote this. I'm aware that urllib2 is the default for this sort of thing now, but I couldn't get my header to work with it properly (otherwise you get a 403 access denied error), so I used requests instead.

import requests
from bs4 import BeautifulSoup

url = 'http://www.grandexchangecentral.com/item.php?rid=4365'
r = requests.get(url, headers={'Referer': 'www.grandexchangecentral.com'})
soup = BeautifulSoup(r.text)
soup.find_all("div", {"class":"CurrentMarket"})

This returns [<div class="CurrentMarket">219</div>], when I would like it to be just 219. Could someone please help me get the proper output? Thanks.


Solution

  • It's pretty easy, assume the return value of your find_all is called markets:

    markets[0].contents[0]
    

    Since markets is a list, get the first item with [0], and then it gets the contents (also a list, thus another [0])

    Maybe look at the docs?