python线程抓取页面简单内容

用到的基本就是之前提到的线程小框架及逻辑

# -*- encoding: utf-8 -*-


import requests
from lxml import etree 
import threading
import os



class Cnbeta(object):

    thread = []

    #获取网站首页
    def get_url(self,url):
        # 如果没有文件就创建文件
        if not os.path.exists('cnbeta.html'):

            res = requests.get(url)
            res1 = res.content.decode("utf-8")  
            with open('./cnbeta.html','w',encoding='utf-8') as f:
                f.write(res1)
            return res1

        else:
            with open('./cnbeta.html',encoding='utf-8') as f:
                content = f.read()
            return content


    # 抓取内页网址
    def get_data(self,html):
        # 完善html标签
        tree = etree.HTML(html)

        url = tree.xpath("//div[@class='headline-thumb']/a/@href")

        for i in url:
            self.thread.append(i)


    # 抓取内页内容放入文件
    def get_xiang(self,res):
        # 动态文件名
        name = str(res).split('/')[-1].replace('.htm','')
        # 请求列表中的地址
        r = requests.get(res)
        r1 = r.content.decode('utf-8')

        path = './upload/'
        with open( path+name+'.html','w',encoding='utf-8' ) as f:
            print("*****")
            f.write(r1)
            


if __name__ == "__main__":

    cnbeta = Cnbeta()

    html = cnbeta.get_url("https://www.cnbeta.com/")

    # 将请求地址放入列表中
    cnbeta.get_data(html)
    print(cnbeta.thread)
    
    for i in cnbeta.thread:
        
        a = threading.Thread(target=cnbeta.get_xiang,args=(i,))
        a.setDaemon(True)
        a.start()
        
    a.join()

爬虫有危险,抓取需谨慎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值