0

I have this script in powershell to get value of visitors at webpage:

$visitors = Invoke-WebRequest -Uri https://footballarena.org
$visitors = $visitors.AllElements | where class -EQ "right" | select -ExpandProperty innertext
$visitors = $visitors -replace '\D+'
$visitors | Export-Csv $env:USERPROFILE\Desktop\export.txt

The output is only numerical value of single DIV class "right"

Now I need to do the same script in python. I can read and store the page:

web = urllib.request.urlopen("https://footballarena.org").read()

Now I need to select value of "161" from this single class:

<div class="right">161 online</div>

I found this question, but I'm not sure how to use it - Python Selenium selecting div class

Could anybody help please?

2
  • Selenium may work too but seems like perfect use case for BeautifulSoup Commented Jan 21, 2017 at 21:44
  • thank you for hint - I will try to study it Commented Jan 21, 2017 at 22:30

1 Answer 1

1

Can do it with BeautifulSoup, install with pip3 install beautifulsoup4, then something like:

from bs4 import BeautifulSoup
import urllib.request

myurl = "https://footballarena.org"

html_doc = urllib.request.urlopen(myurl).read()

soup = BeautifulSoup(html_doc, 'html.parser')

result = soup.findAll("div", { "class" : "right" })

print(result[0].text.split()[0])

outputs:

206 # users currently online

Can probably be improved, but that's the general idea. Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! I've just began with python and you helped me a lot!
Very welcome, glad to be able to help you! Hope your learning python goes well, this was the first time I tried beautifulsoup, but that's because python makes learning new packages easy, quick and fun. Cheers and good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.