以下は、私がウェブスクレイプしたいHTMLコードの抜粋です。与えられた:数字の後ろのテキストではなく、数字だけを削り取るにはどうしたらいいですか?
<tbody>
<tr>
<th>SAT Math</th>
<td>"541 average"</td>
</tr>
</tbody>
私は、Web窮地にPythonと美しいスープを使用して541を抽出するが、私の問題があるのです:
- 私はすべてを取り除くためにどのように「541平均」を抽出くださいたら余分な材料 - 例えばGPAのためにどうすれば "平均"を取り除くことができますか?
ありがとう、私は助けることができる誰にも非常に感謝します!
(申し訳ありませんが、私はPythonとウェブスクレイピングに初心者です)
現在のコード:
import urllib2
from bs4 import BeautifulSoup
import csv
from datetime import datetime
quote_page = 'https://www.collegedata.com/cs/data/college/college_pg02_tmpl.jhtml?schoolId='+str(i)
page = urllib2.urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
table = soup.find("div", attrs={"id":"section8"})
name_box = soup.find('div', attrs={'class': 'cp_left'}).find('h1')
name = name_box.text.strip() # strip() is used to remove starting and trailing
print name
datasets = []
for row in table.find_all("tr")[1:]:
if ((zip(th.get_text() for th in row.find_all("th")))!=[(u'SAT Math',)]):
continue
dataset = zip((th.get_text() for th in row.find_all("th")), (td.get_text() for td in row.find_all("td")))
datasets.append(dataset)
for dataset in datasets:
for field in dataset:
print format(field[1])
あなたはあなたの既存のスープステートメントを表示できますか? –
ようこそ。時間をかけて[ask]とそれに含まれるリンクを読んでください。 [Beautiful Soup Documentation](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#beautiful-soup-documentation) – wwii
こんにちは@PaulaLivingstone私はオリジナルのポストに自分の既存のスープステートメントを追加しました。ありがとう! –