[Python] Wikipedia Crawler
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
# (June 2017 Note: Body nested in two div tags)
content_div = soup.find(id="mw-content-text").find(class_="mw-parser-output") # 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] Wikipedia Crawler的更多相关文章
- Python Web Crawler
Python版本:3.5.2 pycharm URL Parsing¶ https://docs.python.org/3.5/library/urllib.parse.html?highlight= ...
- 【Python五篇慢慢弹】快速上手学python
快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...
- python百科
Python 编辑词条 添加义项名 B 添加义项 ? Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第 ...
- Python in minute
Python 性能优化相关专题: https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/ Python wikipedi ...
- Python网络数据采集7-单元测试与Selenium自动化测试
Python网络数据采集7-单元测试与Selenium自动化测试 单元测试 Python中使用内置库unittest可完成单元测试.只要继承unittest.TestCase类,就可以实现下面的功能. ...
- 深入了解Python
一.Python的风格 Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读.易维护,并且被大量用户所欢迎的.用途广泛的语言. 设计者开发时总的指导思想是,对于一个特定的问题,只要 ...
- ######【Python】【基础知识】Python的介绍 ######
Python 是一种面向对象.解释型计算机程序设计语言. Python是什么? Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言 ...
- 所有selenium相关的库
通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...
- 500lines项目简介
"500行或更少" "What I cannot create, I do not understand." -- Richard Feynman <50 ...
随机推荐
- linux文件时间的查看和改动touch
1. linux文件的时间 linux下文件时间主要有以下三种: 1.1 modification time(mtime) 文件改动时间.即文件内容的改动时,更新这个时间.不包含文件权限和属性的改动. ...
- sklearn 词袋 CountVectorizer
from sklearn.feature_extraction.text import CountVectorizer texts=["dog cat fish","do ...
- 数位$dp$
数位\(dp\)搞了一上午才搞懂.靠这种傻\(X\)的东西竟然花了我一上午的时间. 数位\(dp\) 概念 数位\(dp\)就是强制你分类一些数,例如给你一段区间,然后让你求出不包含\(2\)的数的个 ...
- Asp.Net 中使用 水晶报表(上)
Asp.Net中使用水晶报表(上) 在我们对VS.Net中的水晶报表(Crystal Reports)进行研究之前,我和我朋友对如何将这个复杂的东东加入我们的Web应用有着非常的好奇心.一周以后,在阅 ...
- 文字添加响应事件,js动态加载CSS, js弹出DIV
文字添加响应事件,js动态加载CSS, js弹出DIV <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...
- bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易
题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...
- Linux学习-Ubuntu 18.04-安装图文教程
Ubuntu(友帮拓.优般图.乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球 ...
- 统计 MapReduce 输出路径修改。
先在上一篇MR 的104 行加入代码.jobConf.setOutputFormat(MyMultipleFilesTextOutputFormat.class); 用意是自定义 job 的输出格式: ...
- 简单记录一次REDO文件损坏报错 ORA-00333重做日志读取块出错
一.故障描写叙述 首先是实例恢复须要用到的REDO文件损坏 二.解决方法 1.对于非当前REDO或者当前REDO可是无活动事务使用下面CLEAR命令: 用CLEAR命令重建该日志文件SQL>al ...
- ubuntu 下安装eclipse &java环境配置
前面有一篇的博客写的是ubuntu下安装eclipse和java环境的配置.当时是安装网上的攻略进行的 ,当然也是能够成功的. 近期把那台电脑送人了 ,仅仅好在自己的这台电脑上又一次安装一次了 ,唯一 ...