分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

爬虫学习-urlparse之urljoin()

发布时间:2023-09-06 02:15责任编辑:赖小花关键词:url爬虫

首先导入模块,用help查看相关文档

>>> from urlparse import urljoin>>> help(urljoin)Help on function urljoin in module urlparse:urljoin(base, url, allow_fragments=True) ???Join a base URL and a possibly relative URL to form an absolute ???interpretation of the latter. ???
1
意思就是将基地址与一个相对地址形成一个绝对地址,然而讲的太过抽象

接下来,看几个例子,从例子中发现规律。

>>> urljoin("http://www.google.com/1/aaa.html","bbbb.html")‘http://www.google.com/1/bbbb.html‘>>> urljoin("http://www.google.com/1/aaa.html","2/bbbb.html")‘http://www.google.com/1/2/bbbb.html‘>>> urljoin("http://www.google.com/1/aaa.html","/2/bbbb.html")‘http://www.google.com/2/bbbb.html‘>>> urljoin("http://www.google.com/1/aaa.html","http://www.google.com/3/ccc.html")‘http://www.google.com/3/ccc.html‘>>> urljoin("http://www.google.com/1/aaa.html","http://www.google.com/ccc.html")‘http://www.google.com/ccc.html‘>>> urljoin("http://www.google.com/1/aaa.html","javascript:void(0)")‘javascript:void(0)‘

规律不难发现,但是并不是万事大吉了,还需要处理特殊情况,如链接是其本身,链接中包含无效字符等

1
url = urljoin("****","****")<br><br>### find()查找字符串函数,如果查到:返回查找到的第一个出现的位置。否则,返回-1<br>if url.find("‘")!=-1:<br>    continue  <br><br>### 只取井号前部分<br>url = url.split(‘#‘)[0]<br><br>### 这个isindexed()是我自己定义的函数,判断该链接不在保存链接的数据库中<br>if url[0:4]==‘http‘ and not self.isindexed(url):<br><br>    ###newpages = set(),无序不重复元素集<br>    newpages.add(url)

?

爬虫学习-urlparse之urljoin()

原文地址:https://www.cnblogs.com/php-linux/p/9680193.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved