この機能はurllib2のとBeautifulSoupを含むPythonで何しているのですか?

StackOverflow https://stackoverflow.com/questions/991967

  •  13-09-2019
  •  | 
  •  

質問

だから私は、以前のハイスコアを取得についての質問をしたHTMLページを形成し、他のユーザーが私に助けるために以下のコードを与えました。私は作品によって、いくつかの他のコードピースを通過しようとしているので、私はPythonとbeautifulsoupに新しいです。私はそれのほとんどを理解するが、私はどのようなコードのこの作品は、どのようなその機能はあり得るいけない。

    def parse_string(el):
       text = ''.join(el.findAll(text=True))
       return text.strip()

ここで全体のコードです

from urllib2 import urlopen
from BeautifulSoup import BeautifulSoup
import sys

URL = "http://hiscore.runescape.com/hiscorepersonal.ws?user1=" + sys.argv[1]

# Grab page html, create BeatifulSoup object
html = urlopen(URL).read()
soup = BeautifulSoup(html)

# Grab the <table id="mini_player"> element
scores = soup.find('table', {'id':'mini_player'})

# Get a list of all the <tr>s in the table, skip the header row
rows = scores.findAll('tr')[1:]

# Helper function to return concatenation of all character data in an element
def parse_string(el):
   text = ''.join(el.findAll(text=True))
   return text.strip()

for row in rows:

   # Get all the text from the <td>s
   data = map(parse_string, row.findAll('td'))

   # Skip the first td, which is an image
   data = data[1:]

   # Do something with the data...
   print data 
役に立ちましたか?

解決

el.findAll(text=True)は要素とそのサブ要素内に含まれるすべてのテキストを返します。テキストによって私はありませんタグ内のすべてのものを意味します。そう<b>hello</b>にし、「hello」をテキストが、<b></b>なりません。

この関数は、したがって、一緒に指定された要素とストリップ表裏からオフホワイトスペースの下にあるすべてのテキストを結合します。

ます。http:// WWW

ここでfindAllのドキュメントへのリンクです.crummy.com /ソフトウェア/ BeautifulSoup / documentation.html#のarg-テキスト

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top