使用urllib库

Parsing

The return value from
the urlparse() function
is an object which acts like a tuple with 6 elements.

from urlparse import urlparse

parsed =
urlparse(‘http://netloc/path;parameters?query=argument#fragment')

print parsed

从url中提取域名

方法一:

  1. In [1]: from urllib import parse

  2. In [2]: url =

    https://www.jianshu.com/writer#/notebooks/30425954/notes/43737977'

  3. In [3]: parse.urlparse(url).hostname

  4. Out[3]: ‘www.jianshu.com'

这种方法为从urlparse模块中通过urlparse方法提取url通过hostname属性获取当前url的域名。

方法二:

  1. In [10]: import urllib

  2. In [11]: proto, rest = urllib.splittype(url)

  3. In [12]: host, rest = urllib.splithost(rest)

  4. In [13]: host

  5. Out[13]: ‘www.jianshu.com'

此方法是通过urllib模块中splittype方法先从url中获取到proto协议及rest结果,然后通过splithost从rest中获取到host及rest结果,此时host为域名。(rest被分割了两次)如下图:

 

从域名中提取ip

方法一:

  1. In [14]: from socket import gethostbyname

  2. In [15]: ip_list = gethostbyname(‘www.jianshu.com')

  3. In [16]: ip_list

  4. Out[16]: ‘119.167.250.231’

此方法为从sokcet模块中获取到gethostbyname方法将域名传递进去就能解析出域名的ip。

方法二:

  1. import os

  2. lines=os.popen(‘nslookup blog.csdn.net 221.130.33.52’)

  3. row=lines.readlines()

  4. row

  5. [‘Server:tt221.130.33.52n’,

    ‘Address:t221.130.33.52#53n’, ‘n’, ‘Non-authoritative
    answer:n’, ‘Name:tblog.csdn.netn’, ‘Address:
    47.95.47.253n’, ‘n’]

  6. if len(row) > 5:

  7. … for ip in row[5:]:

  8. … if ‘Address:’ in ip:

  9. … ip=ip.split(‘:’)[1].strip()

  10. … print ip

  11. 47.95.47.253

此方法为通过nslookup获取域名的ip。

注意:

以上从域名中提取ip会不准确,需要设置DNS服务器,这样解析域名就准确了。

Python解析URL参数的简单介绍

在日常工作的项目中,经常需要将一个传递过来的URL进行解析,并拿到其中的某些参数。在Python3中,我们可以使用urllib中的parse来搞定。我们可以拿到请求的协议如http/HTTPS/ftp,还可以拿到路径等等~~~

具体上代码:

import urllib.parse

url = “https://ss.yy.com/pages/viewpage.action?userId=9434&pageId=1"

result = urllib.parse.urlsplit(url)

query = dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(url).query))

ip = urllib.parse.urlsplit(url).netloc

path = urllib.parse.urlsplit(url).path

new_url = urllib.parse.urlparse(url)

print(‘第一、urllib.parse.urlsplit(url)=’, result)

print(‘第二、dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(url).query))=’,
query)

print(‘ip或者域名=’, ip)

print(‘ip或者域名=’, new_url.netloc)

print(‘path路径=’, path)

print(‘userId=’, query[‘userId’], ‘pageId=’,
query[‘pageId’])


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!