环境: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. 如何在linux上查看tomcat的端口号

    1.先到tomcat配置文件查看tomcat的端口是什么,配置文件一般是:$CATALINA_HOME/conf/server这个文件,查找<Connector port="8080& ...

  2. dotnetcore ef 调用多个数据库时用户命令执行操作报错

    dotnetcore ef 调用多个数据库时用户命令执行操作报错 1.多个DbContext 时报错: 报错: More than one DbContext was found. Specify w ...

  3. mysql索引类型(按存储结构划分)

    关于mysql索引类型,网上有很多相关的介绍,给人的感觉很乱.鄙人在翻阅相关书籍后,特意梳理了一下.哪里有不对的地方,欢迎指正! 1. B-Tree索引 它使用B-Tree数据结构来存储数据,实际上很 ...

  4. bootstrap 无需引入 直接使用

    <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="styl ...

  5. 实验1 C语言开发环境使用和数据类型,运算符,表达式

    part :验证性内容 .输出学号. #include<stdio.h> int main(void){ printf("); ; } .输入两个整数,求它们的乘积. #incl ...

  6. Spring AOP 简介

    Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...

  7. bash 和sed 和gawk

    bash内建命令 命令描述 : 扩展参数列表,执行重定向操作 . 读取并执行指定文件中的命令(在当前shell环境中) alias 为指定命令定义一个别名 bg 将作业以后台模式运行 bind 将键盘 ...

  8. 隐藏软键盘(解决自定义Dialog中无法隐藏的问题)

    /** * Dialog中隐藏软键盘不管用 * @param activity */ public static void HideSoftKeyBoard(Activity activity){ t ...

  9. [FJOI2018]领导集团问题 mulitset合并

    P4577 [FJOI2018]领导集团问题 链接 luogu bzoj 他是个重题 bzoj4919: [Lydsy1706月赛]大根堆 代码改改就过了 思路 求树上的lis,要好好读题目的!!! ...

  10. 关于win10环境下Anaconda python,用pip安装包及升级时SSL报错的问题

    刚开始查完以为是网的问题,但是添加信任值\镜像网站\手动下载安装都失败了. 检查后发现python中无SSL模块,检查Anaconda后发现是少加了环境变量Anaconda3\Library\bin, ...