[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 ...
随机推荐
- IP address could not be resolved: Temporary failure in name resolution
今早发现mysql日志中有非常多例如以下的警告: 140724 18:41:25 [Warning] IP address '172.16.18.217' could not be resolved: ...
- hdu5249 Tricks Device(网络流最大匹配)
分析题意可知: 1.最少须要切断多少边使吴不能找到张(题意吴仅仅能走最短路径上面的边),对从起点到终点的最短路径又一次建图,每条边的权值为1.求最大流就可以 2.在吴能够找到张的前提下,最多能够切断边 ...
- HDOJ 4975 A simple Gaussian elimination problem.
和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開 ...
- Nginx中的upstream 分配方法
轮询 轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器,如果某个后端服务器down掉后,能自动剔除. upstream www_cc_com { server 1 ...
- [NOIP2015模拟10.22] 最大子矩阵 解题报告(单调栈)
Description 我们将矩阵A中位于第i行第j列的元素记作A[i,j].一个矩阵A是酷的仅当它满足下面的条件: A[1,1]+A[r,s]<=A[1,s]+A[r,1](r,s ...
- BZOJ 3083 树链剖分+倍增+线段树
思路: 先随便选个点 链剖+线段树 1操作 就直接改root变量的值 2操作 线段树上改 3操作 分成三种情况 1.new root = xx 整个子树的min就是ans 2. lca(new roo ...
- java9新特性-9-语法改进:try语句
1. 使用举例 在java8 之前,我们习惯于这样处理资源的关闭: java 8 中,可以实现资源的自动关闭,但是要求执行后必须关闭的所有资源必须在try子句中初始化,否则编译不通过.如下例所 ...
- mysql的关联查询简写
平常的内连接查询: SELECT * from ab_style as a INNER JOIN ab_url as b on a.style_bold=b.url_id 可支持简写风格: selec ...
- Python中的list,tuple,dict和set
List list的创建与检索 Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 构造list非常简单,直接用 [ ] 把list的所有元素都括 ...
- rsyslog学习
http://blog.csdn.net/zhaoyangjian724/article/details/52116809 http://blog.csdn.net/zhangxihangzhuan/ ...