爬虫实战23:多线程爬取2345电影排行榜

import requests
import bs4
import time
from threading import Thread
from queue import Queue

global my_queue
my_queue = Queue()
start_time = time.time()
print(start_time)


class MyThread1(Thread):
    def __init__(self):
        Thread.__init__(self)
        
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
                         "Chrome/70.0.3538.110 Safari/537.36"}

    def run(self):
        response = requests.get("http://dianying.2345.com/top/", headers=self.headers)
        html = response.text
        soup = bs4.BeautifulSoup(html, "lxml")
        movies_list = soup.find('ul', class_="picList clearfix")
        movies = movies_list.find_all('li')
        for top in movies:
            img_url = "http:" + top.find('img')["src"]
            print(img_url)
            put_data =img_url

            name = top.find('span', class_="sTit").a.text
            my_queue.put((put_data, name))
            try:
                time = top.find('span', class_='sIntro').text
            except:
                time = "暂无上映时间"
            actors = top.find('p', class_="pActor")
            actor = ''
     
            intro = top.find('p', class_="pTxt pIntroShow").text
            print("片名:{}\t{}\n{}\n{} \n \n".format(name, time, actor, intro))


class MyThread2(Thread):
    def __init__(self):
        Thread.__init__(self)
        

    def run(self):
        get_data, name = my_queue.get()
        with open('F:\spider\spider\image\\'+ name +'.png', 'wb+') as f:
            f.write(requests.get(get_data).content)


if __name__ == '__main__':
    thread1 = MyThread1()
    thread2 = MyThread2()
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()
    end_time = time.time()
    print(end_time)
    print(end_time - start_time)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值