爬虫实战(维基xx)

实战网页链接:https://www.britannica.com/topic/Wikipedia

我们可以看见网页文本上有超链接,我们可以在源码看见它们的代码:

我们开始写出简单的爬取,把它的链接全部拿取出来:

python 复制代码
import requests
from bs4 import BeautifulSoup

#https://www.britannica.com/topic/Wikipedia



headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
  'Cookie': '__cf_bm=tib6ICsQJxwj9fpQPz8vhpKt84n0pXOPK6DvV8P8vc4-1713585206-1.0.1.1-3MKOfelDBDRh.z5WLq1ddVtPSuJQ69Ewh9v4Wc.Ljkw9I_G3nUUNOw3W8AG_hXTrF_PojWo.D7_8N4Zn6UGGbw; __mendel=%7B%27pagesViewed%27%3A1%2C%27surveyShown%27%3Afalse%2C%27topicInitialSequence%27%3A0%7D; subreturn=https%3A%2F%2Fwww.britannica.com%2Ferror404; webstats=referer_page=https%3A%2F%2Fwww.britannica.com%2Ferror404'
}

r=requests.get("https://www.britannica.com/topic/Wikipedia")

html=r.text
bsObj=BeautifulSoup(html)

for link in bsObj.findAll("a"):
    if "href" in link.attrs:
        print(link.attrs['href'])

如下:(为爬取成功)

但是我们可以发现有一些是我们不需要的,还有一些URL是重复的,如文章链接,页眉,页脚......

1.URL链接不包括#、=、<、>。

2.URL链接是以/wiki/开头的。

深度优先的递归爬虫:

如下:

python 复制代码
import requests
from bs4 import BeautifulSoup
import re
import time

#https://www.britannica.com/topic/Wikipedia

exist_url=[]#放url
g_writecount=0

def scrappy(url,depth=1):
    global g_writecount
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
            'Cookie': '__cf_bm=tib6ICsQJxwj9fpQPz8vhpKt84n0pXOPK6DvV8P8vc4-1713585206-1.0.1.1-3MKOfelDBDRh.z5WLq1ddVtPSuJQ69Ewh9v4Wc.Ljkw9I_G3nUUNOw3W8AG_hXTrF_PojWo.D7_8N4Zn6UGGbw; __mendel=%7B%27pagesViewed%27%3A1%2C%27surveyShown%27%3Afalse%2C%27topicInitialSequence%27%3A0%7D; subreturn=https%3A%2F%2Fwww.britannica.com%2Ferror404; webstats=referer_page=https%3A%2F%2Fwww.britannica.com%2Ferror404'
        }
        url='https://www.britannica.com/topic/Wikipedia'+url
        r=requests.get(url,headers=headers)
        html=r.text
    except Exception as e:
        print('saveing',url)
        print(e)
        exist_url.append(url)
        return None

    exist_url.append(url)
    link_list=re.findall('<a href="/wiki/([^:#=<>]*?)".*?</a>',html)
    #去重复url
    unique_list=list(set(link_list)-set(exist_url))
    #放到txt文件里面
    for eachone in unique_list:
        g_writecount+=1
        output="NO."+str(g_writecount)+"\t Depth"+str(depth)+"\t"+url+'->'+eachone+'\n'
        print(output)
        with open('link_eqwaak.txt',"a+") as f:
            f.write(output)

        if depth<2:
            scrappy(eachone,depth+1)


scrappy("wiki")

url放入link_eqwaak.txt文件里面,直到深度大于或等于2.

下一节我们实现多线程网页爬取,提高代码的执行速度。

相关推荐
微风中的麦穗13 分钟前
【MATLAB】MATLAB R2025a 详细下载安装图文指南:下一代科学计算与工程仿真平台
开发语言·matlab·开发工具·工程仿真·matlab r2025a·matlab r2025·科学计算与工程仿真
2601_9491465321 分钟前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
开源技术32 分钟前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
学嵌入式的小杨同学38 分钟前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
Li emily1 小时前
解决港股实时行情数据 API 接入难题
人工智能·python·fastapi
wfeqhfxz25887822 小时前
农田杂草检测与识别系统基于YOLO11实现六种杂草自动识别_1
python
mftang2 小时前
Python 字符串拼接成字节详解
开发语言·python
0思必得02 小时前
[Web自动化] Selenium设置相关执行文件路径
前端·爬虫·python·selenium·自动化
石去皿2 小时前
大模型面试通关指南:28道高频考题深度解析与实战要点
人工智能·python·面试·职场和发展
jasligea2 小时前
构建个人智能助手
开发语言·python·自然语言处理