Python 巨蟒-带靓汤的初学者刮痧4-onmouseover

Python 巨蟒-带靓汤的初学者刮痧4-onmouseover,python,beautifulsoup,Python,Beautifulsoup,我是一个python(3)的初学者,目前正试图为我的梦幻足球赛季搜集一些体育统计数据。以前我是以一种迂回的方式(在HT track中下载,转换为excel,并使用VBA组合我的数据)。但现在我正试图学习python来提高我的编码能力 我想勉强凑合,但在仅选择所需的行/表时遇到了一些困难。下面是我的代码目前的状态。它仍然有一些代码,我一直在尝试使用它 from urllib.request import urlopen # import the library from bs4 import B

我是一个python(3)的初学者,目前正试图为我的梦幻足球赛季搜集一些体育统计数据。以前我是以一种迂回的方式(在HT track中下载,转换为excel,并使用VBA组合我的数据)。但现在我正试图学习python来提高我的编码能力

我想勉强凑合,但在仅选择所需的行/表时遇到了一些困难。下面是我的代码目前的状态。它仍然有一些代码,我一直在尝试使用它

from urllib.request import urlopen  # import the library
from bs4 import BeautifulSoup   # Import BS
from bs4 import SoupStrainer    # Import Soup Strainer

page = urlopen('http://www.footywire.com/afl/footy/ft_match_statistics?mid=6172') # access the website
only_tables = SoupStrainer('table') # parse only table elements when parsing
soup = BeautifulSoup(page, 'html.parser')   # parse the html


# for row in soup('table',{'class':'tbody'}[0].tbody('tr')):
#   tds = row('td')
#   print (tds[0].string, tds[1].string)

# create variables to keep the data in

team = []
player = []
kicks = []
handballs = []
disposals = []
marks = []
goals = []
tackles = []
hitouts = []
inside50s = []
freesfor = []
freesagainst = []
fantasy = []
supercoach = []

table = soup.find_all('tr')


# print(soup.prettify())

print(table)
现在,我可以从页面中选择所有“tr”,但仅选择具有以下属性的行时遇到问题:

<tr bgcolor="#ffffff" onmouseout="this.bgColor='#ffffff';" onmouseover="this.bgColor='#cbcdd0';">
从这里我相信我可以把数据放入一个数据框,希望我可以导出到CSV

如果有任何帮助,我将不胜感激,因为我很幸运地查阅了BS4文档。

检查:

正如上面所解释的

你可以使用这个:

table = soup.findAll("tr", {"bgcolor": "#ffffff", "onmouseout": "this.bgColor='#ffffff'", "onmouseover": "this.bgColor='#cbcdd0';"})
此外,您还可以使用以下方法:

tr_tag = soup.findAll(lambda tag:tag.name == "tr" and tag["bgcolor"] == "#ffffff") and tag["onmouseout"] = "this.bgColor='#ffffff'" and tag["onmouseover"] = "this.bgColor='#cbcdd0';"

上述方法的优点是它使用了BS的全部功能,并以非常优化的方式为您提供了结果

非常感谢(和@Dex'ter)!我发誓我也试过类似的方法,但都不管用。非常感谢你的帮助,我现在明白我在ATTR部分做错了什么。
table = soup.findAll("tr", {"bgcolor": "#ffffff", "onmouseout": "this.bgColor='#ffffff'", "onmouseover": "this.bgColor='#cbcdd0';"})
tr_tag = soup.findAll(lambda tag:tag.name == "tr" and tag["bgcolor"] == "#ffffff") and tag["onmouseout"] = "this.bgColor='#ffffff'" and tag["onmouseover"] = "this.bgColor='#cbcdd0';"