Python爬蟲獲取整個站點(diǎn)中的所有外部鏈接代碼示例
收集所有外部鏈接的網(wǎng)站爬蟲程序流程圖

下例是爬取本站python繪制條形圖方法代碼詳解的實(shí)例,大家可以參考下。
完整代碼:
#! /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("http://www.dbjr.com.cn/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("http://www.dbjr.com.cn/article/130968.htm")
爬取結(jié)果如下:

總結(jié)
以上就是本文關(guān)于Python爬蟲獲取整個站點(diǎn)中的所有外部鏈接代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
python logging重復(fù)記錄日志問題的解決方法
python的logging模塊是python使用過程中打印日志的利器,下面這篇文章主要給大家介紹了關(guān)于python logging重復(fù)記錄日志問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
Python編程使用PyQt5庫實(shí)現(xiàn)動態(tài)水波進(jìn)度條示例
這篇文章主要介紹了Python編程使用PyQt5庫實(shí)現(xiàn)動態(tài)水波進(jìn)度條的示例代碼解析,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-10-10
Python內(nèi)置函數(shù)property()如何使用
這篇文章主要介紹了Python內(nèi)置函數(shù)property()如何使用,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09
Python Web框架Pylons中使用MongoDB的例子
這篇文章主要介紹了Python Web框架Pylons中使用MongoDB 的例子,大家參考使用2013-12-12
Numpy中扁平化函數(shù)ravel()和flatten()的區(qū)別詳解
本文主要介紹了Numpy中扁平化函數(shù)ravel()和flatten()的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

