zoukankan      html  css  js  c++  java
  • 爬天极网多线程.py

    #导入多线程模块:
    import threading
    import os
    import requests # 发送请求
    from bs4 import BeautifulSoup # 解析文本
    base_path = os.path.dirname(os.path.abspath(__file__))
    img_path = os.path.join(base_path, 'img')

    def func(a):
    #小f字符串拼接下:
    response = requests.get(f'http://pic.yesky.com/c/6_20491_{a}.shtml')
    soup = BeautifulSoup(response.text, 'html.parser') # 将请求结果交给bs4解析
    div_obj = soup.find(name='div', attrs={"class": "lb_box"}) # 经过分析之后,定位到指定div

    list_dd = div_obj.find_all(name='dd')
    for dd in list_dd: # 每一张图片的dl
    a_obj = dd.find('a')
    # 拼接文件夹的路径,并创建文件夹
    dir_path = os.path.join(img_path, a_obj.text)
    if not os.path.isdir(dir_path): # 判断文件是否存在
    os.mkdir(dir_path)
    a_response = requests.get(a_obj.get('href'))
    a_response.encoding = 'GBK'
    soup2 = BeautifulSoup(a_response.text, 'html.parser')
    div_obj2 = soup2.find(name='div', attrs={"class": "overview"})
    print(div_obj2)
    try:
    img_list = div_obj2.find_all(name='img')
    for img in img_list:
    img_src = img.get("src")
    img_response = requests.get(img_src.replace('113x113', '740x-'))
    file_path = os.path.join(dir_path, img_src.rsplit('/', 1)[-1])
    with open(file_path, 'wb') as f:
    f.write(img_response.content)
    except Exception as e:
    pass

    #循环5圈:
    for i in range(1,6):
    #threading.Thread(target = 函数名,args = (参数,)) #参数必须以元组的形式:
    a = threading.Thread(target=func,args=(i,))
    a.start()

    效果如下:

  • 相关阅读:
    flume,kafka不在一个内网互相打通.md
    尚硅谷Flink2020教程.md
    常用命令.md
    四象限工作效率-事件管理.md
    甘特图目标实施-进度管控.md
    PDCA循环法.md
    SMART大目标拆解小目标.md
    基于内外部竞争环境和竞争条件下的态势分析
    使用Java正则表达式批量提取文本信息
    使用markdown高效编写博客(创建标题)
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12209992.html
Copyright © 2011-2022 走看看