python抓取维基百科,从随机文章开始。点击每篇文章中的第一个链接,看看我们结束的地方!扰流警报:可能在t(代码片段)

author author     2022-12-28     644

关键词:

import time
import urllib

import bs4
import requests


start_url = "https://en.wikipedia.org/wiki/Special:Random"
target_url = "https://en.wikipedia.org/wiki/Philosophy"

def find_first_link(url):
    response = requests.get(url)
    html = response.text
    soup = bs4.BeautifulSoup(html, "html.parser")

    # This div contains the article's body
    content_div = soup.find(id="mw-content-text")

    # stores the first link found in the article, if the article contains no
    # links this value will remain None
    article_link = None

    # Find all the direct children of content_div that are paragraphs
    for element in content_div.find_all("p", recursive=False):
        # Find the first anchor tag that's a direct child of a paragraph.
        # It's important to only look at direct children, because other types
        # of link, e.g. footnotes and pronunciation, could come before the
        # first link to an article. Those other link types aren't direct
        # children though, they're in divs of various classes.
        if element.find("a", recursive=False):
            article_link = element.find("a", recursive=False).get('href')
            break

    if not article_link:
        return

    # Build a full url from the relative article_link url
    first_link = urllib.parse.urljoin('https://en.wikipedia.org/', article_link)

    return first_link

def continue_crawl(search_history, target_url, max_steps=25):
    if search_history[-1] == target_url:
        print("We've found the target article!")
        return False
    elif len(search_history) > max_steps:
        print("The search has gone on suspiciously long, aborting search!")
        return False
    elif search_history[-1] in search_history[:-1]:
        print("We've arrived at an article we've already seen, aborting search!")
        return False
    else:
        return True

article_chain = [start_url]

while continue_crawl(article_chain, target_url):
    print(article_chain[-1])

    first_link = find_first_link(article_chain[-1])
    if not first_link:
        print("We've arrived at an article with no links, aborting search!")
        break

    article_chain.append(first_link)

    time.sleep(2) # Slow things down so as to not hammer Wikipedia's servers

python从维基百科页面中截取所有表格标题(代码片段)

查看详情

python脚本我曾经重命名所有f.r.i.e.n.d.s.通过从维基百科中获取名称来获取epsiodes(代码片段)

查看详情

java示例代码_使用jsoup提取维基百科文章中的特定链接

java示例代码_使用jsoup提取维基百科文章中的特定链接 查看详情

python维基百科摘要(代码片段)

查看详情

随机维基百科阅读器

toopenandreadarandomwikipediaarticleimporturllib2importwebbrowserimportsimplejsonasjson classrandomWikiArticle():curidList=[] defwikiAPI(self):random=urllib2.urlopen("http://en.wikipedia.org/w/api.php?action=query&list=random&rnnamespace=0&rnlim... 查看详情

python维基百科部分循环(代码片段)

查看详情

ruby简单的ruby脚本来获取维基百科今天的精选文章(代码片段)

查看详情

python-bs4-仅使用表头+保存为字典从维基百科表中提取子表(代码片段)

我试图定义一个函数,它提取网站https://de.wikipedia.org/wiki/Stuttgart上的'Basisdaten'表的所有行,并返回一个字典,其键和值对应于表的每一行中的第一个和第二个单元格。'Basisdaten'表是更大表的一部分,如以下代码的结果所示:frombs... 查看详情

随机梯度下降法的数学基础

...续更深入理解随机梯度下降算法的公式。大部分内容来自维基百科和博客文章内容的总结,并加以个人理解。导数导数(英语:​​derivative​​)是微积分学中的一个概念。函数在某一点的导数是 查看详情

c++11特性维基百科总结

C++11标准发布已有一段时间了,维基百科上有对C++11新标准的变化和C++11新特性介绍的文章.我是一名C++程序员,非常想了解一下C++11.英文版的维基百科看起来非常费劲,而中文版维基百科不是知道是台湾... 查看详情

spark实战之:分析维基百科网站统计数据(java版)(代码片段)

...访问我的GitHub在《寻找海量数据集用于大数据开发实战(维基百科网站统计数据)》一文中,我们获取到维基百科网站的网页点击统计数据,也介绍了数据的格式和内容,今天就用这些数据来练习基本的spark开发,<fontcolor="red... 查看详情

高级c#信使(译)-unity维基百科

高级C#信使作者:IlyaSuzdalnitski译自:http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger描述前言MissingReferenceException的原因和解决方案信使用法事件监听器注册事件监听器注销事件监听器广播事件清空信使永久信使杂项打印所有消息从... 查看详情

使用python从程序中抓取模型信息

】使用python从程序中抓取模型信息【英文标题】:Scrapingmodelinformationfromaprogramusingpython【发布时间】:2015-11-1911:16:27【问题描述】:我正在尝试从建筑(Autodesk-Revit)模型中提取物理属性信息(特别是尺寸和阻力值),并将该信... 查看详情

wikipedia是啥

...本身的意思又是什么?我是英语白痴请不吝赐教Wikipedia:维基百科(一个基于wiki技术的多语言的百科全书协作计划,也是一部用不同语言写成的网络百科全书,其目标及宗旨是为全人类提供自由的百科全书);开放性的百科全... 查看详情

xoops-模块-mediawiki维基百科

XOOPS-模块-MediaWiki维基百科克隆维基百科模块修改LocalSettings.php文件本文件修改了MediaWiki数据库表名的前缀,所修改的代码行已用Cloneable标记出。<?php/** *Configfileformediawiki * *ThefileiscomposedofXOOPSbasicpreferencesandmediawikiLocal 查看详情

复数的辐角(维基百科)

    查看详情

python爬虫编程思想(38):项目实战:抓取糗事百科网的段子

        本节的项目会使用requests库抓取糗事百科网的段子,读者可以用下面的URL访问访问糗事百科段子页面。https://www.qiushibaike.com/text页面如图1所示。         在页面... 查看详情

mediawiki怎么标记需要翻译的内容?

RT参考技术AMediaWiki维基百科维基百科[wéijībǎikē]维基百科,是一个基于维基技术的多语言百科全书协作计划,用多种语言编写的网络百科全书。非营利组织维基媒体基金会负责营运维基百科,接受捐赠。2018年12月,维基百科入... 查看详情