环境: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. hiho一下 第207周

    题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...

  2. XUnit测试框架-Python unittest

    选择 语言选择 本次个人作业我选择的语言是Python,了解学习Python有一段时间了但是一直没有练习,所以这次玩蛇,使用的版本是Python3.6. 开发工具选择 我选择的IDE是Pycharm, ...

  3. Java序列化,解决字段为null与序列化后首字母变小写问题

    fastjson.jar package com.apt.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.se ...

  4. VS2010下安装boost库

    在我们的C++项目中安装boost库,下面以VS2010版本作为例子,其它版本的设置也差不多. 一.编译生成boost库 1.下载最新的boost(本人下载的是boost_1_56_0).boost官 ...

  5. EntityFrameworkCore将数据库Timestamp类型在程序中转为long类型

    EntityFrameworkCore将数据库Timestamp类型在程序中转为long类型 EntityFrameworkCore Entity public class Entity { publ ...

  6. Lintcode470-Tweaked Identical Binary Tree-Easy

    470. Tweaked Identical Binary Tree Check two given binary trees are identical or not. Assuming any n ...

  7. EF的优缺点

    优点: 1.简洁的Linq to Sql语句大大提高了开发人员的效率,不要再写复杂的sql语句: 2.不再需要再管应用程序如何去连接数据库: 3.EF可以用作用于数据服务和OData Service的 ...

  8. 【python 3】 字符串方法操作汇总

    基础数据类型:str 1.1  字符串大小写转换 所有字母大写 : string.upper() 所有字母小写 : string. lower() 第一个单词的第一个字母大写,其他字母小写 :  st ...

  9. Tomcat 提示 HTTP Status 500 – Internal Server Error

    错误信息: HTTP Status 500 – Internal Server Error Type Exception Report Message Error instantiating serv ...

  10. 开发者中心没有勾选 ipad却需要传宣传图片的解决方法

    1.通过模拟器 运行一个ipad 把ipad的比例调到100% 然后保存图片,如果没有适配ipad会出现上下左右黑色边框,这些不必在意,把保存的图片拖到开发者中心即可,勾选右侧 ,都使用12.9英寸图 ...