Python爬虫获取整个站点中的所有外部链接代码示例

yipeiwu_com5年前Python爬虫

收集所有外部链接的网站爬虫程序流程图

下例是爬取本站python绘制条形图方法代码详解的实例,大家可以参考下。

完整代码:

#! /usr/bin/env python
#coding=utf-8

import urllib2
from  bs4 import BeautifulSoup
import re
import datetime
import random

pages=set()
random.seed(datetime.datetime.now())
#Retrieves a list of all Internal links found on a page
def getInternalLinks(bsObj, includeUrl):
        internalLinks  =  []
        #Finds all links  that  begin  with  a  "/"
        for link  in bsObj.findAll("a", href=re.compile("^(/|.*"+includeUrl+")")):
                if link.attrs['href'] is not None:
                        if link.attrs['href'] not in internalLinks:
                                internalLinks.append(link.attrs['href'])
        return internalLinks
#Retrieves a list of all external links found on a page
def getExternalLinks(bsObj, excludeUrl):
        externalLinks  =  []
        #Finds all links  that  start  with  "http" or "www"  that  do
        #not  contain the current URL
        for link  in bsObj.findAll("a", 
                              href=re.compile("^(http|www)((?!"+excludeUrl+").)*$")):
                if link.attrs['href'] is not None:
                        if link.attrs['href'] not in externalLinks:
                                externalLinks.append(link.attrs['href'])
        return externalLinks

def splitAddress(address):
        addressParts  =  address.replace("http://", "").split("/")
        return addressParts

def getRandomExternalLink(startingPage):
        html=  urllib2.urlopen(startingPage)
        bsObj  =  BeautifulSoup(html)
        externalLinks  =  getExternalLinks(bsObj, splitAddress(startingPage)[0])
        if len(externalLinks) == 0:
                internalLinks  =  getInternalLinks(startingPage)
                return internalLinks[random.randint(0, len(internalLinks)-1)]
        else:
                return externalLinks[random.randint(0, len(externalLinks)-1)]

def followExternalOnly(startingSite):
        externalLink=getRandomExternalLink("/post/130968.htm")
        print("Random  external  link  is: "+externalLink)
        followExternalOnly(externalLink)

#Collects a list of all external URLs found on the site
allExtLinks=set()
allIntLinks=set()
def getAllExternalLinks(siteUrl):
    html=urllib2.urlopen(siteUrl)
    bsObj=BeautifulSoup(html)
    internalLinks  =  getInternalLinks(bsObj,splitAddress(siteUrl)[0])
    externalLinks  =  getExternalLinks(bsObj,splitAddress(siteUrl)[0])
    for link in externalLinks:
      if link not in allExtLinks:
        allExtLinks.add(link)
        print(link)
    for link in internalLinks:
      if link not in allIntLinks:
        print("About to get link:"+link)
        allIntLinks.add(link)
        getAllExternalLinks(link)

getAllExternalLinks("/post/130968.htm")

爬取结果如下:

总结

以上就是本文关于Python爬虫获取整个站点中的所有外部链接代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python解决网站的反爬虫策略总结

本文详细介绍了网站的反爬虫策略,在这里把我写爬虫以来遇到的各种反爬虫策略和应对的方法总结一下。 从功能上来讲,爬虫一般分为数据采集,处理,储存三个部分。这里我们只讨论数据采集部分。 一...

python3写爬取B站视频弹幕功能

python3写爬取B站视频弹幕功能

需要准备的环境: 一个B站账号,需要先登录,否则不能查看历史弹幕记录 联网的电脑和顺手的浏览器,我用的Chrome Python3环境以及request模块,安装使用命令,换源比较快:...

Python爬虫实现使用beautifulSoup4爬取名言网功能案例

本文实例讲述了Python爬虫实现使用beautifulSoup4爬取名言网功能。分享给大家供大家参考,具体如下: 爬取名言网top10标签对应的名言,并存储到mysql中,字段(名言,...

python抓取网页内容示例分享

复制代码 代码如下:import socketdef open_tcp_socket(remotehost,servicename):    s=socke...

python爬虫之模拟登陆csdn的实例代码

python模拟登陆网页主要使用到urllib、urllib2、cookielib及BeautifulSoup等基本模块,当然进阶阶段我们还可以使用像requests等更高级一点的模块。...