多线程爬虫-BeautifulSoup

线程基类

import threading
class base_thread(threading.Thread):
    def __init__(self, func):
        threading.Thread.__init__(self)
        self.func = func
        #print(type(self.func))
    def run(self):
        self.func

爬虫百度贴吧中某帖子图片

  • 先要安装BeautifulSoup
import urllib.request as request
from bs4 import BeautifulSoup
from Base.Threads import base_thread
path = 'e:/apps/pic/'
def CrawlerFunc(url):
    list_img = getUrl(url)
    multi_thread(len(list_img), downloadImg(list_img))
def getUrl(url):
    response = request.urlopen(url)
    html = response.read()
    data = html.decode('utf-8')
    soup = BeautifulSoup(data)
    list_img = []
    for list in soup.find_all("img", {"class", "BDE_Image"}):
        list_img.append(list.attrs["src"])
    return list_img

def downloadImg(list_img):
    count = 1
    for i in list_img:
        filepath = path + str(count)+".jpg"
        with open(filepath, 'wb') as file:
            print(filepath)
            image_data = request.urlopen(i).read()
            file.write(image_data)
        count += 1
def multi_thread(count, func):
    threads = []
    for i in range(0, count):
        threads.append(base_thread(func))
    for j in range(0, count):
        threads[j].start()
    for k in range(0, count):
        threads[k].join()
#CrawlerFunc("http://tieba.baidu.com/p/3764230390")
参考
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,462评论 25 708
  • GitHub 上有一个 Awesome - XXX 系列的资源整理,资源非常丰富,涉及面非常广。awesome-p...
    若与阅读 18,758评论 4 418
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,587评论 6 427
  • 亲爱的大树, 生日快乐!今天是你的生日,我看到了姐姐发过来的照片,照片上的你看起来精神极了。照片上的蛋糕也好诱人啊...
    间晓阅读 672评论 0 0
  • 你轻颦,我痛然 你浅笑,我悦然
    雪慕阅读 298评论 1 2