Итак, я задал вопрос ранее о получении высоких результатов с 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