python-爬虫-爬取fuchsia源码

本文介绍了一个使用Python编写的爬虫程序,用于从Fuchsia中文社区的镜像站点爬取源码及相关数据。该程序利用urllib和BeautifulSoup库解析网页,递归地遍历目标网站的所有链接,下载并保存数据到本地指定目录。注意,爬取的数据可能不完整,无法直接用于编译。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 爬虫
--python 爬取fuchsia中文社区提供网站上面的源码等数据,仅供参考--
import os
import sys
import re
import urllib
import urllib.request
import urllib.response
import urllib.error
import ssl
from bs4 import BeautifulSoup
import html5lib
from collections import deque

quene = deque()

url = "https://mirrors.hexang.com/fuchsia/"

cur_path = os.path.abspath('.') + "\\fuchisa"

types = []

def add_type(str):

    if str not in types:
        types.append(str)
    return types

quene.append(url)
while quene:
    url_ = quene.popleft()
    print("url open : ",url_)
    try:
        op = urllib.request.urlopen(url_)
    except urllib.error.HTTPError as e:
        print(" 404 Not found ")
    except urllib.error.URLError as e:
        print(e.code)
    finally:
        print("others exception")

     # 找不到的直接pass掉   
    if op.status != 200:
        continue

    data = op.read()

    soup = BeautifulSoup(data,"html5lib")
    href = ''
    for href in soup.find_all('a'):
        text = href.get_text()
        if text != "../":
            url_1 = url_+ href.get_text()
            if url_1:
                quene.append(url_1)

    #input()
    split_str = url_.split('/')
    index = split_str.index("fuchsia")
    split_str = split_str[index + 1:len(split_str)]
    
    sub_modules = []
    for i in split_str:
        if i !='':
            sub_modules.append(i)
        else:
            pass
        
    print("sub_moudles : ",sub_modules)
    if len(sub_modules) != 0:
        sub_path = ''
        for i in sub_modules:
            sub_path += i
            sub_path += "\\"
            print(sub_path)
    else:
        sub_path = ''

    print("sub_path : ",sub_path)
    tp = op.getheader("Content-type")
    print(tp)
    types = add_type(tp)
    print("types : ",types)
    #这里可以根据不同的数据类型进行不同数据处理
    if "html" in op.getheader("Content-type"):
        path = cur_path +"\\"+ sub_path
        print("mkdir : ",path)
        if not os.path.exists(path):
            os.makedirs(path)
    else:
        sub_path = sub_path[:-1]
        abs_path = cur_path + "\\" + sub_path
        print("save file ",abs_path)
        with open(abs_path,"wb") as f:
            f.write(data)
            f.close()

数据爬取下来是不完整的,编译不了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值