爬虫实战(维基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.

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

相关推荐
Wx-bishekaifayuan5 小时前
springboot户外登山社交小程序19787-计算机课程设计、毕业设计
spring boot·后端·python·spring·elasticsearch·django·课程设计
FYKJ_20105 小时前
springboot刑事案件管理系统03047-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
Python图像识别6 小时前
18-【2027毕设】YOLO11烟雾检测识别系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·深度学习·yolo·毕业设计·毕设
FYKJ_20106 小时前
springboot助农产品销售商城05829-计算机课程设计、毕业设计
java·spring boot·python·mysql·spark·django
Yunovian6 小时前
AI时代,针对模型与应用,浅谈一下各编程语言
开发语言·c++·人工智能·python·ai·rust·ai编程
weixin_457760007 小时前
基于 YOLO11m-Pose 的动物姿态估计系统:从 Animal-Pose 标注到 Python 桌面部署
人工智能·python·深度学习
Ticnix7 小时前
RAG 烂大街?烂大街的只是那条流水线——真正的分水岭在这五处
后端·python·agent
Wx-bishekaifayuan7 小时前
springboot社区扶贫救助管理系统13300-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
周杰伦fans7 小时前
构建真正智能的CAD AI绘图代理
开发语言·人工智能·c#
小小张说故事8 小时前
LightGBM 入门指南:更快的梯度提升树,Python 实战
后端·python·机器学习