广度优先遍历——爬虫的python3实现

本文介绍如何使用Python3进行广度优先遍历来实现网页爬虫。在jupyter notebook环境下,从起始页https://www.cnblogs.com/goodhacker/p/3353146.html出发,目标网址为http://book.51cto.com/art/201012/236668.htm。在从Python2到Python3的转换过程中,遇到了函数变化的问题,通过查阅Python官方文档得以解决。

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

参考:https://www.cnblogs.com/goodhacker/p/3353146.html

采用广度优先搜索,使用python3语言进行网页爬虫

实验工具:jupyter notebook

起始页网址:https://www.cnblogs.com/goodhacker/p/3353146.html

目标网址:http://book.51cto.com/art/201012/236668.htm

源代码:

#encoding=utf-8
from bs4 import BeautifulSoup
import socket 
import urllib.request as request
import zlib 
import re
      
class MyCrawler:  
    def __init__(self,seeds):  
            #使用种子初始化url队列  
            self.linkQuence=linkQuence()  
            if isinstance(seeds,str):  
                self.linkQuence.addUnvisitedUrl(seeds)  
            if isinstance(seeds,list):  
                for i in seeds:  
                    self.linkQuence.addUnvisitedUrl(i)  
            print("Add the seeds url \"%s\" to the unvisited ur
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值