我遇到了以下代码,我发现这些代码非常有用,但不确定如何解释它的一部分。
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:]]
任何人都可以解释这是做什么以及我应该如何阅读它?谢谢!
答案 0 :(得分:1)
这一行基本上可以分解为此。
for count, row in enumerate(rows[1:]):
for header, td in zip(headers, row.find_all('td'):
results[title][count][header] = td.text