成都创新互联网站制作重庆分公司

python爬虫urljoin()如何生成链接

这篇文章给大家分享的是有关python爬虫urljoin()如何生成链接的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

嫩江网站建设公司创新互联公司,嫩江网站设计制作,有大型网站制作公司丰富经验。已为嫩江近千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的嫩江做网站的公司定做!

urlunparse()和urlunsplit()方法,我们可以完成链接的合并,但前提是必须有一个特定长度的对象,链接的每个部分都应该清晰地分开。除此之外还有urljoin()方法。

1、urljoin()可以实现生成链接。提供base_url(基本链接)作为第一个参数,以新链接作为第二个参数。

2、该方法将分析base_url的scheme、netloc和path,并补充新链接缺失的部分,最终返回结果。

实例

from urllib.parse import urljoin
 
print(urljoin('http://www.baidu.com', 'FAQ.html'))
print(urljoin('http://www.baidu.com', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html?question=2'))
print(urljoin('http://www.baidu.com?wd=abc', 'https://cuiqingcai.com/index.php'))
print(urljoin('http://www.baidu.com', '?category=2#comment'))
print(urljoin('www.baidu.com', '?category=2#comment'))
print(urljoin('www.baidu.com#comment', '?category=2'))

输出

http://www.baidu.com/FAQ.html
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html
https://cuiqingcai.com/FAQ.html?question=2
https://cuiqingcai.com/index.php
http://www.baidu.com?category=2#comment
www.baidu.com?category=2#comment
www.baidu.com?category=2

感谢各位的阅读!关于“python爬虫urljoin()如何生成链接”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


网页标题:python爬虫urljoin()如何生成链接
文章链接:http://cxhlcq.com/article/igppsc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部