python爬虫之解析链接

解析链接

1. urlparse() & urlunparse()

urlparse() 是对url链接识别和分段的,API用法如下:

urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)

他的三个参数:

  • urlstring: 这是一个必须项,即待解析的url。
  • scheme: 它是默认协议。假如这个链接没有带协议信息,会将这个作为默认协议。
from urllib.parse import urlparse

result = urlparse('www.baidu.com/index.html;user?id=5#comment', scheme='https')
print(result)

结果为:

ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')
  • allow_fragments: 即是否忽略fragment。如果他被设置为Falsefragment部分就会被忽略,他会解析为pathparameters或者query的一部分,而fragment部分为空
from urllib.parse import urlparse

result = urlparse('http://www.baidu.com/index.html;user?id=5#comment', allow_fragments=False)
print(result)

结果如下:

ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5#comment', fragment='')

假设URL中不包含paramsquery,如下:

from urllib.parse import urlparse

result = urlparse('http://www.baidu.com/index.html#comment', allow_fragments=False)
print(result)

运行结果如下:

ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html#comment', params='', query='', fragment='')

可以发现,当URL中不包含paramsquery时,fragment便会被解析为path的一部分。返回结果ParseResult实际上是一个元组,我们可以用索引顺去来获取,也可以用属性名获取。如下:

from urllib.parse import urlparse

result = urlparse('http://www.baidu.com/index.html#comment', allow_fragments=False)
print(result.scheme, result[0], result.netloc, result[1], sep='\n')

结果如下:

http
http
www.baidu.com
www.baidu.com

可以发现二者结果一致

urlunparse()是其对立方法,他可接收一个可迭代对象。但是长度必须是6,否则会抛出参数数量不足或者过多的问题。如下:

from urllib.parse import urlunparse

data = ['http', 'www.baidu.com', 'index.html', 'user', 'a=6', 'comment']
print(urlunparse(data))

结果如下:

http://www.baidu.com/index.html;user?a=6#comment

2. urlsplite() & urlunsplite()

unsplite()urlparse()方法相似。但是不能单独解析params这一部分,只返回5个结果。

from urllib.parse import urlsplit

result = urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
print(result)

结果:

SplitResult(scheme='http', netloc='www.baidu.com', path='/index.html;user', query='id=5', fragment='comment')

返回结果SplitResult是一个元组类型,既可以用属性获取值,也可以用索引来获取。如下:

from urllib.parse import urlsplit

result = urlsplit('http://www.baidu.com/index.html;user?id=5#comment')
print(result.scheme, result[0])

结果:

http http

urlunsplit()urlunparse()方法类似,他也是将链接各个部分组合成完整链接的方法。传入的参数也是一个可迭代对象。长度必须为5,如下:

from urllib.parse import urlunsplit

data = ['http', 'www.baidu.com', 'index.html', 'a=6', 'comment']
print(urlunsplit(data))

结果:

http://www.baidu.com/index.html?a=6#comment

3. urljoin()

生成链接的另外一个方法,可以提供一个base_url作为第一个参数,将新的链接作为第二个参数,该方法会分析base_urlschemenetlocpath 这3个内容并对链接缺失部分进行补充。如下:

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

base_url提供了三项内容schemenetlocpath。 如果这3项在新的链接里不存在就会补充。如果新的链接存在,就使用新的链接的部分。而base_url中的paramsqueryfragment是不起作用的。

4. urlencode()

urlencode()方法在构造GET请求参数的时候非常有用,如下:

from urllib.parse import urlencode

params = {
    'name': 'germey',
    'age': 22
}
base_url = 'http://www.baidu.com?'
url = base_url + urlencode(params)
print(url)

首先声明一个字典来作为参数表示,然后调用urlencode()方法将其序列化为GET请求参数。

http://www.baidu.com?name=germey&age=22

5. parse_qs()

这是一个反序列化方法,我们可以利用parse_qs()方法,就可以转回字典,如下:

from urllib.parse import parse_qs

query = 'name=germey&age=22'
print(parse_qs(query))

结果

{'name': ['germey'], 'age': ['22']}

6. parse_qsl()

将参数转化成元组列表

from urllib.parse import parse_qsl

query = 'name=germey&age=22'
print(parse_qsl(query))

结果:

[('name', 'germey'), ('age', '22')]

7. quote() & unquote()

quote()是将中文转换成URL编码,unquote()是对URL进行解码。

from urllib.parse import quote

keyword = '壁纸'
url = 'https://www.baidu.com/s?wd=' + quote(keyword)
print(url)
https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8
from urllib.parse import unquote

url = 'https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8'
print(unquote(url))
https://www.baidu.com/s?wd=壁纸

参考崔庆才博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值