环境:pycharm

尝试对地址进行切片 去掉头 http 或 https

a.遇到了一些问题

url = 'https://www.cnblogs.com/derezzed/articles/8119592.html'
#检查协议
protocl = "http"
if url[:7] =="http://":
u = url.split('://')[1]
elif url[:8] == "https://":
protocl = "https"
u = url.split("://")
else:
u = url
print(u)

发现无任何输出

url = 'https://www.cnblogs.com/derezzed/articles/8119592.html'
#检查协议
protocl = "http"
if url[:7] =="http://":
u = url.split('://')[1]
print(u)
elif url[:8] == "https://":
protocl = "https"
u = url.split("://")
print(u)
else:
u = url
print(u)

修改后看到了结果 至于为何 暂不知道原因

b.按着教程 边理解 边写出的解析url程序 (此程序有问题)

#url = 'http://movie.douban.com/top250'
#解析url 返回一个tuple 包含 protocol host path port def parsed_url(url):
#检查协议 protocol = 'http'
if url[:7] == 'http://':
a = url.split('://')[1] elif url[:8] == 'https://':
a = url.split('https://')[1]
protocol = 'https' #检查默认path
i = a.find('/')
if(i == -1):
path = '/'
host = a
else:
host = a[:16]
path = a[6:] #检查端口
port_dict = {
'http': 80,
'https' : 443,
}
#默认端口
port = port_dict[protocol]
if ':' in host:
h = host.split(':')
host = h[0]
port = int (h[1]) return protocol, host, port, path
 

写完后发现编译器一直报错 对着源程序反复确认还是找不到问题所在 一直报错

“python illegal target for variable annotation”

最后发现是缩进问题 详情查看我的 python learn 或者搜索python的缩进要求

而后加入测试程序

def test_parsed_url():
"""
parsed_url 函数很容易出错, 所以我们写测试函数来运行看检测是否正确运行
"""
http = 'http'
https = 'https'
host = 'g.cn'
path = '/'
test_items = [
('http://g.cn', (http, host, 80, path)),
('http://g.cn/', (http, host, 80, path)),
('http://g.cn:90', (http, host, 90, path)),
('http://g.cn:90/', (http, host, 90, path)),
#
('https://g.cn', (https, host, 443, path)),
('https://g.cn:233/', (https, host, 233, path)),
]
for t in test_items:
url, expected = t
u = parsed_url(url)
# assert 是一个语句, 名字叫 断言
# 如果断言成功, 条件成立, 则通过测试, 否则为测试失败, 中断程序报错
e = "parsed_url ERROR, ({}) ({}) ({})".format(url, u, expected)
assert u == expected, e

发现还是不对 虽然报错很明显 但依然不知道错在哪里 冷静下来观察错误

这里看到 错误已经很明显 给出了来源 自己写的(其实是仿着写的qwq)所以没有意识到自己写的expected函数连正确答案都显示出来了 (汗)

最终 马上意识到 host path是有问题的 将 i 替换成了具体数字 修改后错误消失

error 0 了 啊太棒了 !!! 加油~~~

python web1(解析url)的更多相关文章

  1. Python 的 urllib.parse 库解析 URL

      Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...

  2. Python - Django - 命名 URL 和反向解析 URL

    命名 URL: test.html: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  3. Python 文本解析器

    Python 文本解析器 一.课程介绍 本课程讲解一个使用 Python 来解析纯文本生成一个 HTML 页面的小程序. 二.相关技术 Python:一种面向对象.解释型计算机程序设计语言,用它可以做 ...

  4. Python XML解析之ElementTree

    参考网址: http://www.runoob.com/python/python-xml.html https://docs.python.org/2/library/xml.etree.eleme ...

  5. python dpkt解析ssl流

    用法:python extract_tls_flow.py -vr  white_pcap/11/2018-01-10_13-05-09_2.pcap  -o pcap_ssl_flow.txt  & ...

  6. urlparse模块(专门用来解析URL格式)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #urlparse模块(专门用来解析URL格式) #URL格式: #protocol ://hostname[ ...

  7. 关于Python json解析过程遇到的TypeError: expected string or buffer

    关于Python json解析过程遇到的问题:(爬取天气json数据所遇到的问题http://tianqi.2345.com/) part.1 url——http://tianqi.2345.com/ ...

  8. Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间

    Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间 一丶URLconf配置 ...

  9. python抽取指定url页面的title方法

    python抽取指定url页面的title方法 今天简单使用了一下python的re模块和lxml模块,分别利用的它们提供的正则表达式和xpath来解析页面源码从中提取所需的title,xpath在完 ...

随机推荐

  1. [最新]ABP ASP.NET Zero v5.5.2 破解

    参考https://www.cnblogs.com/xajh/p/8428818.html后, 搞定了Abp.AspNetZeroCore.dll.Abp.AspNetZeroCore.Web.dll ...

  2. 在linux上安装python, jupyter, 虚拟环境(virtualenv)以及 虚拟环境管理之virtualenvwraper

    一, 安装python31.下载python3源码 wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz2.解压缩源码包,去 ...

  3. ACTIVEMQ 实例化到MSSQL

    实例化文章很多,不重复,自行查询 直接上XML <!-- Licensed to the Apache Software Foundation (ASF) under one or more c ...

  4. Tomcat笔记 #01# WEB应用管理工具简介

    索引 查看JVM以及SERVLET/接口的情况 动态管理WEB应用 Tomcat自带了一个基于网页的web应用管理工具,可以帮助我们监控&管理部署上去的WEB APP,特别方便!恰好之前碰到的 ...

  5. Yii1操作phpexcel

    Yii::import('application.vendors.phpexcel.*'); Yii::import('application.vendors.phpexcel.PHPExcel.*' ...

  6. setInterval中this

    今天使用react做钟表,自然用到了setInterval,但是出现this指向不明的问题. <html> <head> <meta charset="UTF- ...

  7. Oracle 12c 单实例安装

    准备工作 实验环境:Redhat 6.6   Oracle 12c 12.2.0.1 1.官网下载 https://www.oracle.com/technetwork/database/enterp ...

  8. bzoj 4767 两双手 - 动态规划 - 容斥原理

    题目传送门 传送门I 传送门II 题目大意 一个无限大的棋盘上有一只马,设马在某个时刻的位置为$(x, y)$, 每次移动可以将马移动到$(x + A_x, y + A_y)$或者$(x + B_x, ...

  9. 02_计算机网络的OSI七层(应表会传网数物)

    七层: 应用层 表示层 会话层 传输层 网络层 数据链路层 物理层 五层: 应用层 传输层 网络层 数据链路层 物理层 四层: 应用层 传输层 网络层 数据接口层 一.物理层(Physical Lay ...

  10. 旧版本firefox添加扩展addons的地址

    不要在 firefox 本身的addons 中去查找, 搜索, 那个是搜索不到的, 因为那个是针对 最新版的, 旧版本的很多插件都不能用, 被移除了, 要在 那个专门 提供 插件的站点中去寻找扩展 h ...