最後の質問(here)に感謝してくれてありがとう。しかし、私は現在、私の最終的なデータフレームを準備しています。私は元のテーブルからすべてのデータを抽出してそれを表示することができましたが、現在はホームチームとアウェイチームをdfに追加したいが、それを把握することはできないようだ。ここには私が現在持っているものがあり、here is the site私は掻きたいです。BeautifulSoup 4をPythonで掻き集める - 初心者
from urllib.request import urlopen # import the library
from bs4 import BeautifulSoup # Import BS
from bs4 import SoupStrainer # Import Soup Strainer
import pandas as pd # import pandas as a package
basescrape = 'http://www.footywire.com/afl/footy/ft_match_statistics?mid='
matchid = '6172'
scrapeweb1 = basescrape+matchid
page = urlopen(scrapeweb1) # access the website
only_tables = SoupStrainer('table', attrs={"width" : "583"}) # parse only table elements when parsing
soup = BeautifulSoup(page, 'html.parser', parse_only=only_tables) # parse the html
only_teams = SoupStrainer('table', attrs={"width" : "376"}) # parse only team qtr score elements when parsing
soup2 = BeautifulSoup(page, 'html.parser', parse_only=only_teams) # parse the html
# only valid rows with player data in
table = soup.find_all("tr", attrs={"onmouseover" : "this.bgColor='#cbcdd0';"})
# create variables to keep the data in
hometeam = []
awayteam = []
player = []
kicks = []
handballs = []
disposals = []
marks = []
goals = []
behinds = []
tackles = []
hitouts = []
inside50s = []
freesfor = []
freesagainst = []
fantasy = []
supercoach = []
# Find all the <tr> tag pairs, skip the first one, then for each.
for row in soup.find_all("tr", attrs={"onmouseover" : "this.bgColor='#cbcdd0';"}):
# Create a variable of all the <td> tag pairs in each <tr> tag pair,
col = row.find_all('td')
# Create a variable of the string inside 1st <td> tag pair,
column_1 = col[0].string.strip()
# and append it to player variable
player.append(column_1)
# Create a variable of the string inside 2nd <td> tag pair,
column_2 = col[1].string.strip()
# and append it to kicks variable
kicks.append(column_2)
# Create a variable of the string inside 3rd <td> tag pair,
column_3 = col[2].string.strip()
# and append it to handballs variable
handballs.append(column_3)
# Create a variable of the string inside 4th <td> tag pair,
column_4 = col[3].string.strip()
# and append it to disposals variable
disposals.append(column_4)
# Create a variable of the string inside 5th <td> tag pair,
column_5 = col[4].string.strip()
# and append it to marks variable
marks.append(column_5)
# Create a variable of the string inside 5th <td> tag pair,
column_6 = col[5].string.strip()
# and append it to goals variable
goals.append(column_6)
# Create a variable of the string inside 5th <td> tag pair,
column_7 = col[6].string.strip()
# and append it to behinds variable
behinds.append(column_7)
# Create a variable of the string inside 5th <td> tag pair,
column_8 = col[7].string.strip()
# and append it to tackles variable
tackles.append(column_8)
# Create a variable of the string inside 5th <td> tag pair,
column_9 = col[8].string.strip()
# and append it to hitouts variable
hitouts.append(column_9)
# Create a variable of the string inside 5th <td> tag pair,
column_10 = col[9].string.strip()
# and append it to inside50s variable
inside50s.append(column_10)
# Create a variable of the string inside 5th <td> tag pair,
column_11 = col[10].string.strip()
# and append it to freesfo variable
freesfor.append(column_11)
# Create a variable of the string inside 5th <td> tag pair,
column_12 = col[11].string.strip()
# and append it to freesagainst variable
freesagainst.append(column_12)
# Create a variable of the string inside 5th <td> tag pair,
column_13 = col[12].string.strip()
# and append it to fantasy variable
fantasy.append(column_13)
# Create a variable of the string inside 5th <td> tag pair,
column_14 = col[13].string.strip()
# and append it to supercoach variable
supercoach.append(column_14)
# Find all the <tr> tag pairs, then for each.
for row in soup2.find_all("tr", class_= "leftbold"):
# Create a variable of all the <td> tag pairs in each <tr> tag pair,
col2 = row.find_all('td')
# Create a variable of the string inside 1st <td> tag pair,
hometeam = col2[0].string.strip()
# and append it to player variable
# hometeam.append(column2_1)
# Create a variable of the string inside 2nd <td> tag pair,
awayteam = col2[1].string.strip()
# and append it to kicks variable
# awayteam.append(column2_2)
# Create a variable of the value of the columns
columns = {'match_id': matchid, 'home_team': hometeam, 'away_team': awayteam, 'player': player, 'kicks': kicks, 'handballs': handballs, 'disposals': disposals, 'marks': marks, 'goals': goals, 'behinds': behinds, 'tackles': tackles, 'hitouts': hitouts, 'inside_50s': inside50s, 'frees_for': freesfor, 'frees_against': freesagainst, 'fantasy': fantasy, 'supercoach': supercoach}
# Create a dataframe from the columns variable - n
df = pd.DataFrame(columns, columns = ['match_id', 'home_team', 'away_team', 'player', 'kicks', 'handballs', 'disposals', 'marks', 'goals', 'behinds', 'tackles', 'hitouts', 'inside_50s', 'frees_for', 'frees_against', 'fantasy', 'supercoach'])
print(df)
# print(soup.prettify())
# print(table)
明らかに、配列はすべて同じ長さではないため、データフレームは機能しません。ホームと離れたチームを擦って、それを変数に保存して、マッチドイドと同じように動作させるにはどうすればいいですか?
「hometeam」変数を最初の22行に表示させ、23-44行目の「離れた場所」に表示させる方法がありますか?そうすれば、プレーヤーは1つのチームに帰属しますか?
は、私は私が間違ってここにこのセクションをやっている感じ:あなたの助けを
# Find all the <tr> tag pairs, then for each.
for row in soup2.find_all("tr", class_= "leftbold"):
# Create a variable of all the <td> tag pairs in each <tr> tag pair,
col2 = row.find_all('td')
# Create a variable of the string inside 1st <td> tag pair,
hometeam = col2[0].string.strip()
# and append it to player variable
# hometeam.append(column2_1)
# Create a variable of the string inside 2nd <td> tag pair,
awayteam = col2[1].string.strip()
# and append it to kicks variable
# awayteam.append(column2_2)
どうもありがとうございました。
(また、 "+"を文字列に使用しているので、 ".join"演算子を使用してscrapeweb1を動作させることはできませんでした。それは "ではなく「/ TD」を使用しています
<table border="0" cellspacing="0" cellpadding="0" width="376" id="matchscoretable">
<tr>
<th class="leftbold" height="23" width="100">Team</td>
...ので、私は、ソースを検査し、そのテーブルの一部の間違ったHTMLがあります表示されます。失敗した、
scrapeweb1 = "".join(basescrape, matchid)
編集)以下であります/ th "と表示され、美しいスープでパースされるとテーブルタグが閉じます。
[<table border="0" cellpadding="0" cellspacing="0" id="matchscoretable" width="376">
<tr>
<th class="leftbold" height="23" width="100">Team</th></tr></table>]
私はこの問題を解決するために管理
おかげで、それは私の理解では、これは素晴らしいデータフレーム内のすべてのデータや場所、それを掻き取るのか? 編集:私はちょうどそれを実行するためにいくつかの時間を得ることができた、そんなにうんざりです!あなたの助けをありがとう。このような痛みをおかけして申し訳ありませんが、私は各ラインを理解するのに役立つでしょうか?私は必死に私のpythonのコーディングを改善しようとしています。 – MSalty
これまでのところ理解しています(「players_list = []」を1行目として使用) 1.「players_list」という空の変数を作成します 2.「チーム」内の各チームを見ますBS要素 3.チーム名は最初の値が返されます(チーム名) 4.スクラップしたい実際のテーブルの行である新しいBS要素を定義します 5.各行のヘッダーを割り当てますDFのラベルを作成する 6.テーブルの各行を通過するループを作成する 7.別のループを作成して各列の値をヘッダーに割り当て、 "player_dict"に保存する... – MSalty
8.取得各 'td'ブラケットの各値を正しいラベルに格納します 9。"team"という名前の新しい列を作成し、それを行に割り当てます。 10.完了した行を "player_list"変数に追加します。 11.プレイヤーリストのデータフレームを作成します これはすべて正しいですか?私は誰かを逃したのですか? もう一度ありがとうございます。 – MSalty