如何使用漂亮的HTML页面从网页解析表格?

时间:2018-02-06 14:23:38

标签: python python-3.x web-scraping beautifulsoup

我试图解析此网站表格内容,我尝试使用以下程序

    import requests
from bs4 import BeautifulSoup

url = "http://www.espncricinfo.com/rankings/content/page/211270.html"
page = requests.get(url)
soup = BeautifulSoup(page.text,"html.parser")
batsman_type = (soup.find_all('h3'))[0].text
ret = []
row = {}


for tr in soup.find_all("tr"):

        tds = tr.find_all("td")
        if len(tds) > 0 :
            row = {'Rank':tds[0].text,'Name': tds[1].text, 'Country' : tds[2].text, 'Rating': tds[3].text}
            ret.append(row)
            row = {}
ret.append(row)
print(ret)

当我打印td时它没有返回任何值,我怎么能解决这个问题以获取表的所有内容?如果你需要任何其他问题请随时询问

0 个答案:

没有答案