标签搜索
侧边栏壁纸
博主昵称
2c

  • 累计撰写 19 篇文章
  • 累计收到 12 条评论

python 爬虫

2c
2c
2022-04-28 / 4 评论 / 16 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年05月23日,已超过698天没有更新,若内容或图片失效,请留言反馈。

在这里插入图片描述

import re
import os
import requests

global i
i = 0


def get_one_page(url):
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = response.text
    return html


def get_urls(html):
    pattern = re.compile('href="(/touxiang/qinglv/20\d+/\d+\.html)"', re.S)
    urls = re.findall(pattern, html)
    return urls


def get_pic_url(html):
    pattern = re.compile('href="(//img\d\.woyaogexing\.com/20\d\d.*?\.jpeg)"', re.S)
    pic_urls = re.findall(pattern, html)
    return pic_urls


def save_pic(url, pic_path):
    global i
    if not os.path.exists(pic_path):
        os.mkdir(pic_path)
    with open(os.path.join(pic_path, str(i) + '.jpg'), 'wb') as f:
        # f.write(requests.get(url).content)
      print(url)
    i += 1


def main():
    html = get_one_page('https://www.woyaogexing.com/touxiang/qinglv/new/')
    urls = get_urls(html)
    for url in urls:
        sub_html = get_one_page('https://www.woyaogexing.com' + url)
        pic_urls = get_pic_url(sub_html)
        for pic_url in pic_urls:
            save_pic('http:' + pic_url, 'D:\\test\\')


if __name__ == '__main__':
    main()
0

评论 (4)

取消
  1. 头像
    north
    Windows 10 · FireFox

    感谢博主

    回复
  2. 头像
    2c 作者
    Windows 10 · Google Chrome

    哈哈哈

    回复
  3. 头像
    2c 作者
    Windows 10 · Google Chrome

    测试

    回复
  4. 头像
    Windows 10 · Google Chrome

    我来啦

    回复