2

I am a newby in python programming and I wanted to practice webscraping. My original aim was to list top 10 players in some countries according their points. But it seems, I cannot get the whole source code of the page, hence I am not able to create my database. So when I click in Chrome on "inspect\elements", I am able to see the necessary data, but with my code below, I cannot get all of it.

from selenium import webdriver
url = 'https://aoe2.net/#aoe2de-leaderboard-rm-1v1'
driver = webdriver.Chrome()
code = driver.get(url)
source = driver.page_source
print(source)

Could you tell me please, what do I do wrong?

1 Answer 1

2

tried out this code and should work:

from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
code = driver.get('https://aoe2.net/#aoe2de-leaderboard-rm-1v1')
sleep(5) #Since my internet is slow, if yours is fast enough then you can comment this line
d1 = {}
for i in range(1,11):
    if i % 2 == 1:
        val = 'odd'
    else:
        val = 'even'
    a = driver.find_element_by_css_selector(f'tr.{val}:nth-child({i}) > td:nth-child(3) > a:nth-child(3)').text
    d1[i] = a
print(d1)

Just replace firefox with chrome, since I didn't have the Chrome Drivers Installed

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

3 Comments

Thank you! That really works. However I have some trouble to understand this part of your code: "> td:nth-child(3) > a:nth-child(3)" What the ">" and the (3) stands for exactly? Could you please explain it to me?!
Hey There Thats the css selector script, Doing inspect element on the name, and copying for the name column's CSS selector code. tr.{val}:nth-child({i}) > td:nth-child(3) > a:nth-child(3) can be split up into parts: tr - table row (html) val refers to odd and even, this was a repeating pattern in the CSS Selector (odd for odd place and vice versa) nthchild{i}- refers to the actual ranking number (1,2,3,4) the td:nth-child(3) as per my understanding is the location of the data inside the cell. Since the cell which contained name also had blank spacing as well as hyperlink and...
avatar information, we took only selective data

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.