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
ooleslie
V2EX  ›  Python

正在爬取豆瓣相册,但是不知道为什么,这个代码每个相册都会遍历两边,很苦恼

  •  
  •   ooleslie · 2018-08-23 15:13:43 +08:00 · 1160 次点击
    这是一个创建于 2089 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/usr/bin/env python3

    import re
    import urllib.request
    import os

    headers = ('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36')
    opener = urllib.request.build_opener()
    opener.addheaders = [headers]


    def obtain_piclist(link):
    #创建文件夹并爬取对应的照片
    page_info = opener.open(link).read()
    page_info = str(page_info, encoding = 'utf-8')
    title_match = r'title>mochizukikaoru 的相册-([\s\S]+)?</title>'
    title = re.compile(title_match).findall(page_info)
    title = title[0].replace
    result_dir = os.path.join('/Users/tnb/Pictures/Douban/',str(title).replace('/','|'))
    if not os.path.exists(result_dir):
    os.makedirs(result_dir)
    pic_rule = '<img width="201" src="( https://img3.doubanio.com/view/photo/m/public/.+?.jpg)" />'
    pic_add = re.compile(pic_rule).findall(page_info)
    x = 1
    for pic_link in pic_add:
    pic_name = result_dir + '/' + '_'+str(x)+'.jpg'
    try:
    urllib.request.urlretrieve(pic_link, pic_name)
    urllib.request.urlcleanup()
    except urllib.error.URLError as e:
    if hasattr(e, 'code'):
    x += 1
    if hasattr(e, 'reason'):
    x += 1
    x += 1


    def obtain_page_pic(url):
    #爬取每页的相册地址
    page_url = str(opener.open(url).read())
    rule = 'div class="wr".+?class="clear"'
    page = str(re.compile(rule).findall(page_url))
    rule_2 = r'href="( https://www.douban.com/photos/album/.+?/)"'
    page_2 = re.compile(rule_2).findall(page)
    page_2 = list(page_2)
    for link in page_2:
    obtain_piclist(link)
    print(link)


    if __name__ == '__main__':
    for i in range(0,99):
    #获取相册目录和创建相册文件夹
    url = 'https://www.douban.com/people/mochizukikaoru/photos?start='+str(i*18)
    obtain_page_pic(url)
    ooleslie
        1
    ooleslie  
    OP
       2018-08-23 15:38:44 +08:00
    已经解决啦,还有一个问题,相册中有‘/’符号,但是还想原版的创建这个名字的相册,有哪些方法可以用呢?因为 os.path 会读取'/',很苦恼
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2500 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:01 · PVG 00:01 · LAX 09:01 · JFK 12:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.