Python网络数据采集2-wikipedia

随机链接跳转

获取维基百科的词条超链接,并随机跳转。可能侧边栏和低栏会有其他链接。这不是我们想要的,所以定位到正文。正文在idbodyContentdiv标签里。

import random
import re
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/52.0.2743.116 Safari/537.36 Edge/15.16193'}

start_url = '/wiki/Wiki'

def get_links(url):
    r = requests.get('https://en.wikipedia.org' + url, headers=headers)
    soup = BeautifulSoup(r.text, 'lxml')
    # /wiki/some_words
    link_list = soup.find('div', id='bodyContent').find_all('a', href=re.compile(r'^/wiki/[^/]*$'))
    return link_list

links = get_links(start_url)
while len(links) > 0:
    # 随机选择一个链接
    link = random.choice(links).get('href')
    print(link)
    # 新的词条覆盖了原来的超链接,一直搜寻
    links = get_links(link)
/wiki/Personal_wiki
/wiki/Database_management_system
/wiki/Netezza
/wiki/C%2B%2B
/wiki/C%2B%2B#Standardization
/wiki/ISO_9984
/wiki/Georgian_script
...

从首页开始,将首页的所有词条放入集合中(去重),再遍历集合,从集合中的链接递归搜索。

import re
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/52.0.2743.116 Safari/537.36 Edge/15.16193'}

pages = set()

def get_links(url):
    global pages
    r = requests.get('https://en.wikipedia.org' + url, headers=headers)
    soup = BeautifulSoup(r.text, 'lxml')
    # /wiki/some_words
    link_list = soup.find('div', id='bodyContent').find_all('a', href=re.compile(r'^/wiki/[^:/]*$'))
    for link in link_list:
        if link['href'] not in pages:
            new_page = link['href']
            pages.add(new_page)
            print(new_page)
            get_links(new_page)

if __name__ == '__main__':
    # 空字符串表示,url为wiki主页https://en.wikipedia.org
    get_links('')

获取词条的标题、正文

标题在h1标签中,正文在id为mw-content-text的div标签中。

import re
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/52.0.2743.116 Safari/537.36 Edge/15.16193'}

pages = set()

def get_links(url):
    global pages
    r = requests.get('https://en.wikipedia.org' + url, headers=headers)
    soup = BeautifulSoup(r.text, 'lxml')
    # /wiki/some_words
    try:
        print(soup.h1.string)
        # 只打印第一段
        print(soup.find(id='mw-content-text').find('p').text)
    except AttributeError:
        print('页面缺少一些属性。')

    link_list = soup.find('div', id='bodyContent').find_all('a', href=re.compile(r'^/wiki/[^:/]*$'))
    for link in link_list:
        if link['href'] not in pages:
            new_page = link['href']
            pages.add(new_page)
            print('----------\n' + new_page)
            get_links(new_page)

if __name__ == '__main__':
    # 空字符串表示,url为wiki主页https://en.wikipedia.org
    get_links('')
Main Page
Noye's Fludde is a one-act opera written largely for young amateur performers, created by the British composer Benjamin Britten. First performed in 1958 at the annual Aldeburgh Festival, it is based on the 15th-century Chester "mystery" play which recounts the biblical story of Noah, the flood and the ark. Britten had written numerous works for mixed
...
--------
/wiki/Wikipedia
Wikipedia
Wikipedia (/ˌwɪkᵻˈpiːdiə/ ( listen) or /ˌwɪkiˈpiːdiə/ ( listen) WIK-i-PEE-dee-ə) is a free online encyclopedia with the aim to allow anyone to edit articles.[3] Wikipedia is the largest and most popular general reference work on the Internet[4][5][6] and is ranked among the ten most popular websites.[7] Wikipedia is owned by the nonprofit Wikimedia Foundation.[8][9][10]
--------
/wiki/Main_Page
...

寻找外链

https://www.oreilly.com开始不断寻找外链,如果某个页面没有外链,则进入该页面的某个内链,再重新找外链。感觉这个例子不是很好,因为从其他外链又可能回到初始页面。

import re
import random
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/52.0.2743.116 Safari/537.36 Edge/15.16193'}

def get_random_external_link(start_page):
    r = requests.get(start_page, headers=headers)
    soup = BeautifulSoup(r.text, 'lxml')
    # 返回分割地址的第一个元素,一般是主页的地址
    ex_links = get_external_links(soup, split_address(start_page)[0])
    # 如果该页面没有外链,则获取内链,再从内链里随机选取一个,递归,直到获取到外链为止。
    if len(ex_links) == 0:
        internal_links = get_internal_links(soup, split_address(start_page)[0])
        return get_random_external_link(random.choice(internal_links))
    else:
        return random.choice(ex_links)

def get_internal_links(bs, include_url):
    internal_links = []
    # 找出所有以为'/'开头的链接,此为内链
    in_links = bs.find_all('a', href=re.compile(r'^/|' + include_url))
    for link in in_links:
        if link['href'] not in internal_links:
            internal_links.append(link['href'])

    return internal_links

def get_external_links(bs, exclude_url):
    external_links = []
    # 找出所有以http、https开头的链接,且不含内链字符的,此为外链,(?!...)表示不包含
    ex_links = bs.find_all('a', href=re.compile(r'^(https|http)((?!' + exclude_url + ').)*$'))
    for link in ex_links:
        if link['href'] not in external_links:
            external_links.append(link['href'])

    return external_links

def split_address(address):
    address_parts = []

    if address.split(':')[0] == 'http':
        address_parts = address.replace('http://', '').split('/')
    elif address.split(':')[0] == 'https':
        address_parts = address.replace('https://', '').split('/')

    return address_parts

# 只搜索外链
def follow_external_only(url):
    external_link = get_random_external_link(url)
    print(external_link)
    follow_external_only(external_link)

all_ex_links = set()
all_in_links = set()

# 获得所有外链和内链,并打印了外链
def get_all_external_links(url):
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, 'lxml')
    internal_links = get_internal_links(soup, split_address(url)[0])
    external_links = get_external_links(soup, split_address(url)[0])
    for link in external_links:
        if link not in all_ex_links:
            all_ex_links.add(link)
            print(link)

    for link in internal_links:
        if link not in all_in_links:
            all_in_links.add(link)
            get_all_external_links(link)

if __name__ == '__main__':
    # follow_external_only('https://www.oreilly.com')
    get_all_external_links('https://www.oreilly.com')
https://www.safaribooksonline.com/?utm_medium=content&utm_source=oreilly.com&utm_campaign=lgen&utm_content=20170601+nav
http://shop.oreilly.com/
http://members.oreilly.com
https://www.safaribooksonline.com/?utm_medium=content&utm_source=oreilly.com&utm_campaign=lgen&utm_content=20170505+homepage+get+started+now
https://www.safaribooksonline.com/accounts/login/?utm_medium=content&utm_source=oreilly.com&utm_campaign=lgen&utm_content=20170203+homepage+sign+in
https://www.safaribooksonline.com/?utm_medium=content&utm_source=oreilly.com&utm_campaign=lgen&utm_content=20170710+homepage+get+started+now
https://www.safaribooksonline.com/public/free-trial/?utm_medium=content&utm_source=oreilly.com&utm_campaign=lgen&utm_content=20170710+homepage+start+free+trial
https://www.safaribooksonline.com/accounts/login/?utm_medium=content&utm_source=oreilly.com&utm_campaign=lgen&utm_content=20170710+homepage+sign+in
...

上面的代码经常会出错,可能是正则表达式匹配的原因,也有可能是网络原因。


by @sunhaiyu

2017.7.14

Python网络数据采集2-wikipedia的更多相关文章

  1. 笔记之Python网络数据采集

    笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ...

  2. Python网络数据采集7-单元测试与Selenium自动化测试

    Python网络数据采集7-单元测试与Selenium自动化测试 单元测试 Python中使用内置库unittest可完成单元测试.只要继承unittest.TestCase类,就可以实现下面的功能. ...

  3. Python网络数据采集3-数据存到CSV以及MySql

    Python网络数据采集3-数据存到CSV以及MySql 先热热身,下载某个页面的所有图片. import requests from bs4 import BeautifulSoup headers ...

  4. [python] 网络数据采集 操作清单 BeautifulSoup、Selenium、Tesseract、CSV等

    Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesseract.CSV等 Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesse ...

  5. Python网络数据采集6-隐含输入字段

    Python网络数据采集6-隐含输入字段 selenium的get_cookies可以轻松获取所有cookie. from pprint import pprint from selenium imp ...

  6. Python网络数据采集4-POST提交与Cookie的处理

    Python网络数据采集4-POST提交与Cookie的处理 POST提交 之前访问页面都是用的get提交方式,有些网页需要登录才能访问,此时需要提交参数.虽然在一些网页,get方式也能提交参.比如h ...

  7. Python网络数据采集1-Beautifulsoup的使用

    Python网络数据采集1-Beautifulsoup的使用 来自此书: [美]Ryan Mitchell <Python网络数据采集>,例子是照搬的,觉得跟着敲一遍还是有作用的,所以记录 ...

  8. Python网络数据采集PDF

    Python网络数据采集(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/16c4GjoAL_uKzdGPjG47S4Q 提取码:febb 复制这段内容后打开百度网盘手 ...

  9. python网络数据采集的代码

    python网络数据采集的代码 https://github.com/REMitchell/python-scraping

随机推荐

  1. tcp/ip详解 卷1 -- 协议概述

    第一章 概述 分层 TCP/IP 通常被认为是一个四层协议系统. 每一层负责不同的功能. 链路层, 也成为数据链路层或者网络接口层. 通常包括 操作系统中的设备驱动程序和计算机中对应的网络接口卡. 主 ...

  2. position定位和添加阴影

    定位:style="position: absolute; bottom: 0; width: 100%;background: rgb(255, 255, 255);padding-bot ...

  3. Linux文件锁定保护命令chattr介绍

    chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files- 最关键的是在[mode]部分,[mode]部分是由+-=和[ASacDdIijsTt ...

  4. 2~62位任意进制转换(c++)

    进制转换的符号表为[0-9a-zA-Z],共61个字符,最大可表示62进制. 思路是原进制先转换为10进制,再转换到目标进制. 疑问: 对于负数,有小伙伴说可以直接将符号丢弃,按照整数进行进位转换,最 ...

  5. ps命令注意事项

    1.ps命令由于历史原因,版本比较多,主要分为三种版本 1)Unix风格的版本,命令参数加单横线.比如ps -ef 2)BSD风格的版本,命令参数前不加任何横线.比如ps aux 3)GNU风格的版本 ...

  6. 2.vue 安装教程

    安装node.js 从node.js官网下载并安装node,安装过程很简单,一路"下一步"就可以了(傻瓜式安装). 安装完成之后,打开命令行工具,输入 node -v,如下图,如果 ...

  7. centos7 minimal版本下mysql的安装

    最近第一次尝在虚拟机上安装mysql,由于是centos7 minimal版本,很多安装包或命令必须自己添加,遇到很多问题. 首先是执行# yum install mysql-server 报错: 打 ...

  8. OSX MacVim + vim-lldb配置和使用心得

    Mac里面默认的编译器是clang/clang++ 所以debugger就选择了lldb 想搭配MacVim一起使用,于是就找到了vim-lldb这个插件,相当强大   这个插件支持Vundle,所以 ...

  9. AutoMapper 6.x 扩展

    简介 很多时候我们使用AutoMapper的时候,都需要进行一个配置才可以使用Mapper.Map<Source,Target>(entity);.如果不进行配置则会报错. 如果实体过多, ...

  10. [UWP]浅谈按钮设计

    一时兴起想谈谈UWP按钮的设计. 按钮是UI中最重要的元素之一,可能也是用得最多的交互元素.好的按钮设计可以有效提高用户体验,构造让人眼前一亮的UI.而且按钮通常不会影响布局,小小的按钮无论怎么改也不 ...