Im trying to parse the table present in the [website][1]
[1]: http://www.espncricinfo.com/rankings/content/page/211270.html using selenium, as I am beginner . i'm struggling to do that here is my code
from bs4 import BeautifulSoup
import time
from selenium import webdriver
url = "http://www.espncricinfo.com/rankings/content/page/211270.html"
browser = webdriver.Chrome()
browser.get(url)
time.sleep(3)
html = browser.page_source
soup = BeautifulSoup(html, "lxml")
print(len(soup.find_all("table")))
print(soup.find("table", {"class": "expanded_standings"}))
browser.close()
browser.quit()
that I tried, I'm unable to fetch anything from this, any suggestions will be really helpful thanks
It looks like that page's tables are within iframes. If you have a specific table you want to scrape, try inspecting it using browser dev tools (right click, inspect element in Chrome) and find the iframe element that is wrapping it. The iframe should have a
src
attribute that holds a url to the page that actually contains that table. You can then use a similar method to the one you tried but instead use thesrc
url.Selenium can also "jump into" an iframe if you know how to find the iframe in the page's source code.
frame = browser.find_element_by_id("the_iframe_id") browser.switch_to.frame(frame) html = browser.page_source
etcThe table you are after is within an
iframe
. So, to get the data from that table you need to switch thatiframe
first and then do the rest. Here is one way you could do it:And the best approach would be in this very case is as follows. No browser simulator is used. Only
requests
andBeautifulSoup
have been used:Partial results: