时间:2021-05-22
收集所有外部链接的网站爬虫程序流程图
下例是爬取本站python绘制条形图方法代码详解的实例,大家可以参考下。
完整代码:
#! /usr/bin/env python#coding=utf-8import urllib2from bs4 import BeautifulSoupimport reimport datetimeimport randompages=set()random.seed(datetime.datetime.now())#Retrieves a list of all Internal links found on a pagedef 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 pagedef getExternalLinks(bsObj, excludeUrl): externalLinks = [] #Finds all links that start with "http" or "pile("^(http|www)((?!"+excludeUrl+").)*$")): if link.attrs['href'] is not None: if link.attrs['href'] not in externalLinks: externalLinks.append(link.attrs['href']) return externalLinksdef splitAddress(address): addressParts = address.replace("http://", "").split("/") return addressPartsdef 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 siteallExtLinks=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爬虫获取整个站点中的所有外部链接代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
第一个方法就是用jquery实现,原理是这样的先找到站点中的外部链接,然后再给链接加上target=_blank”属性就搞定了,代码如下:jquery在新窗口打
站点外部仅是外部链接。外部链接的方法很多,例如友谊链接或软件文章,带签名的论坛,博客等。这里重要的亮点是外部链接的前提质量和外部链接的质量链的平台重量将影响整个
python爬虫-梨视频短视频爬取(线程池)示例代码importrequestsfromlxmlimportetreeimportrandomfrommulti
通用网络爬虫的目标在整个互联网中,爬虫从种子URL开始访问网页,采集当中所有超链接。为了防止获取重复的URL,将爬取到的网页信息存储在原始数据库或队列中,然后对
准备首先安装爬虫urllib库pipinstallurllib获取有道翻译的链接url需要发送的参数在formdata里示例importurllib.reque