试着理解Python靓汤解析鳕鱼

2024-04-24 08:10:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现下面的代码非常有用,但不知道如何解释其中的一部分。你知道吗

from pprint import pprint
import urllib2
from bs4 import BeautifulSoup

url = 'http://en.wikipedia.org/wiki/List_of_Bollywood_films_of_2014'
soup = BeautifulSoup(urllib2.urlopen(url))

headers = ['Opening', 'Title', 'Genre', 'Director', 'Cast']
results = {}

for block in soup.select('div#mw-content-text > h3'):
    title = block.find('span', class_='mw-headline').text
    rows = block.find_next_sibling('table', class_='wikitable').find_all('tr')

    results[title] = [{header: td.text for header, td in zip(headers, row.find_all('td'))}
                      for row in rows[1:]]

pprint(results)

除了这一块我都懂:

    results[title] = [{header: td.text for header, td in zip(headers, row.find_all('td'))}
                      for row in rows[1:]]

有人能解释一下这是做什么的,我应该怎么读吗?谢谢!你知道吗


Tags: textinimportfortitleallfindblock