看不懂这里怎样实现翻页?有大神可以解答一下吗?谢谢。

2017-12-22 21:52:59 +08:00
 andmspy

def parse_html(html): soup = BeautifulSoup(html, 'lxml') movie_list_soup = soup.find('ol', attrs = {'class': 'grid_view'}) movie_name_list = []

for movie_li in movie_list_soup.find_all('li'):
    detail = movie_li.find('div', attrs = {'class': 'hd'})
    movie_name = detail.find('span', attrs = {'class': 'title'}).getText()
    movie_name_list.append(movie_name)
print(movie_name_list)

next_page = soup.find('span', attrs = {'class': 'next'}).find('a')

if next_page:
    return movie_name_list, DOWNLOAD_URL + next_page['href']

return movie_name_list, None
2305 次点击
所在节点    Python
5 条回复
sean10
2017-12-22 23:38:09 +08:00
倒数第二行不是返回了下一页的 url 么,在其他函数里有写继续爬的逻辑吧
lihongjie0209
2017-12-22 23:55:52 +08:00
if next_page:
// 返回一个 tuple(movie_name_list, next_page_url)
return movie_name_list, DOWNLOAD_URL + next_page['href']

return movie_name_list, None


这个应该是链接提取函数, 如果有下一页就把下一页的链接作为 tuple 的第二项返回.
andmspy
2017-12-24 21:56:11 +08:00
谢谢 @lihongjie0209
@lihongjie0209 如果返回链接下一页页面,不需要 requests.get 请求一下么?
return 就可以直接返回了么?
lihongjie0209
2017-12-24 21:58:07 +08:00
@andmspy 这个是链接提取函数, 不是下载函数, 用这个函数提取链接之后再下载
andmspy
2017-12-24 22:07:31 +08:00
@lihongjie0209
因为‘ DOWNLOAD_URL + next_page['href'] ’ 这个就是下一页的链接所以在 def 函数里面,return 就是可以实现链接下一页,是这个意思么?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/416946

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX