使用循环读取多个网页

时间:2015-02-09 16:37:33

标签: python for-loop

我使用一个函数(movies_from_url)从网页上读取总共256个电影。每页包含50部电影。我必须为此阅读前6页(250页电影5页,6电影6页)。

第一个网址:

http://www.imdb.com/search/title?at=0&sort=user_rating&start=1&title_type=feature&year=2005,2014

这是我模糊的想法:

def read_m_by_rating(first_year=2005, last_year=2015, top_number=256):
    current_index=1   # current index is start number  of a webpage 
    final_list = []
    for _ in xrange(6):
    url = http://www.imdb.com/search/title?at=0&sort=user_rating&start=current_index&title_type=feature&year=2005,2014
    if top_number==300:
         lis = movies_from_url(url, top_number - current_index + 1)
    else:
         lis = movies_from_url(url, 50)

    final_list.append(lis)
    current_index=+50
    return final_list

1 个答案:

答案 0 :(得分:1)

只需在current_index上使用一个简单的循环即可。

while current_index<256:
    url = "http://www.imdb.com/search/title?at=0&sort=user_rating&start="\
    +str(current_index)+"&title_type=feature&year=2005,2014"
    ...
    ...
    current_index+=50
return final_list