gfro_za gfro_za - 2 months ago 8
Python Question

Webscraper in Python - How do I extract exact text I need?

Good day

I am trying to write my first webscraper. I have managed to write the following:

import requests
from bs4 import BeautifulSoup

s = requests.Session()

r = s.get("http://www.sharenet.co.za/v3/quickshare.php?scode=BTI")

r = s.post("http://www.sharenet.co.za/v3/quickshare.php?scode=BTI")

soup = BeautifulSoup(r.text, "html.parser")
print(soup.find_all("td", class_="dataCell"))


I am trying to extract a share price. When Inspect the element this is the HTML code:

<td class="dataCell" align="right">85221</td>


Image of share price table

Basically, my issue is that can search for all the tags but can't extract the exact tag I want.

Thanks in advance for any help.

Answer

Tags have a get_text() method. find_all returns a list of tags.

for cell_tag in soup.find_all("td"):
    print(cell_tag.get_text())