维基百科网络爬虫案例研究

维基爬虫实践

维基百科网络爬虫案例研究

一、HTML源代码的构成

HTML源代码由嵌套标签组成。

  1. 第一个标签是标题标签,<title>和结束标签</title>之间的文本是页面标题。
  2. 下一个标签是<div id="introduction">,以</div>作为结束标签。
  3. p是段落的缩写,<p>和结束标签</p>之间的文本是显示在文本上的内容。p是div标签的子类,div是p段落的父类。
  4. 该html文档中,第二个div较为复杂。它有一个子类的段落标签pp段落标签有自己的字类imga。这两个标签是div标签的后代标签,不是div的子类,而是p段落标签的子类。
  5. 锚标签:用<a></a>表示,用于创建链接。href可以指定链接。

二、简单页面的源码

<title>My Website</title>
<div id="introduction">
  <p>
    Welcome to my website!
  </p>
</div>    
<div id="image-gallery">
  <p>
    This is my cat!
    <img src="cat.jpg" alt="Meow!">
    <a href="https://en.wikipedia.org/wiki/Cat">Learn more about cats!</a>
  </p>
</div>

三、使用python获取HTML

安装请求库resquests

import requests
response = requests.get('https://en.wikipedia.beta.wmflabs.org/wiki/Dog')
html = response.text
print(html)
print(type(html))

四、解析HTML

常用库:Beautiful Soup文档描述

五、思路

编写continue_crawl函数。函数功能:

  1. search_history是维基百科文章 url 的字符串列表。列表中的最后一个项目是最近发现的 url。
  2. 如果target_url是查找到结果,停止搜索时文章 url 的字符串。

如果 search_history 中最近的文章 == 目标文章,则停止搜索,函数返回 False
如果列表中有 25 个 url,函数返回 False
如果列表中有一个循环,函数返回 False
否则继续搜索,函数返回 True。

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值