圆月山庄资源网 Design By www.vgjia.com

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

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("https://www.jb51.net/article/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("https://www.jb51.net/article/130968.htm")

爬取结果如下:

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

总结

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

标签:
python爬虫代码示例,python,爬虫代码,python简单爬虫代码,python爬虫实例代码

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。