V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
andmspy
V2EX  ›  Python

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

  •  
  •   andmspy · 2017-12-22 21:52:59 +08:00 · 2295 次点击
    这是一个创建于 2310 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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
    
    5 条回复    2017-12-24 22:07:31 +08:00
    sean10
        1
    sean10  
       2017-12-22 23:38:09 +08:00 via iPhone
    倒数第二行不是返回了下一页的 url 么,在其他函数里有写继续爬的逻辑吧
    lihongjie0209
        2
    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
        3
    andmspy  
    OP
       2017-12-24 21:56:11 +08:00
    谢谢 @lihongjie0209
    @lihongjie0209 如果返回链接下一页页面,不需要 requests.get 请求一下么?
    return 就可以直接返回了么?
    lihongjie0209
        4
    lihongjie0209  
       2017-12-24 21:58:07 +08:00
    @andmspy 这个是链接提取函数, 不是下载函数, 用这个函数提取链接之后再下载
    andmspy
        5
    andmspy  
    OP
       2017-12-24 22:07:31 +08:00
    @lihongjie0209
    因为‘ DOWNLOAD_URL + next_page['href'] ’ 这个就是下一页的链接所以在 def 函数里面,return 就是可以实现链接下一页,是这个意思么?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1223 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:55 · PVG 07:55 · LAX 16:55 · JFK 19:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.