python web1(解析url)
环境: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)的更多相关文章
- Python 的 urllib.parse 库解析 URL
Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...
- Python - Django - 命名 URL 和反向解析 URL
命名 URL: test.html: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- Python 文本解析器
Python 文本解析器 一.课程介绍 本课程讲解一个使用 Python 来解析纯文本生成一个 HTML 页面的小程序. 二.相关技术 Python:一种面向对象.解释型计算机程序设计语言,用它可以做 ...
- Python XML解析之ElementTree
参考网址: http://www.runoob.com/python/python-xml.html https://docs.python.org/2/library/xml.etree.eleme ...
- 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 & ...
- urlparse模块(专门用来解析URL格式)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #urlparse模块(专门用来解析URL格式) #URL格式: #protocol ://hostname[ ...
- 关于Python json解析过程遇到的TypeError: expected string or buffer
关于Python json解析过程遇到的问题:(爬取天气json数据所遇到的问题http://tianqi.2345.com/) part.1 url——http://tianqi.2345.com/ ...
- Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间
Django---路由系统,URLconf的配置,正则表达式的说明(位置参数),分组命名(捕获关键字参数),传递额外的参数给视图,命名url和url的反向解析,url名称空间 一丶URLconf配置 ...
- python抽取指定url页面的title方法
python抽取指定url页面的title方法 今天简单使用了一下python的re模块和lxml模块,分别利用的它们提供的正则表达式和xpath来解析页面源码从中提取所需的title,xpath在完 ...
随机推荐
- vue单文件组件实例2:简单单文件组件
Introduce.vue: <template> <div class="intro"> 单位介绍 </div> </templat ...
- Python数据分析Pandas库之熊猫(10分钟一)
pandas熊猫10分钟教程 排序 df.sort_index(axis=0/1,ascending=False/True) df.sort_values(by='列名') import numpy ...
- jquery的on()用法实例
首先,先看官方描述: 再来,用实例解释一下: 1.简单绑定单个事件: $("body").on("click",".edit_btn",fu ...
- 【python】文件处理行与行之间的内容
在处理文本文件时,很多时候需要我们处理跨行的数据,但是用for循环处理不是很方便,想了一个歪招来处理不是很大的数据. 核心思想就是将上一行的东西存在一个列表里,到下一行用完这个数据在循环体里将列表初始 ...
- 下拉框click事件与搜索框blur事件的爱恨纠葛
还原车祸现场 功能类似于百度搜索,搜索框输入内容,下拉框显示候选项,点击候选项就选择候选项,然后下拉框隐藏,点击外面就直接隐藏下拉框,于是我写了以下代码 //参会单位联想 $('input[name= ...
- vue/iview使用moment.js
方法一 main.js引入moment 获取当前时间 this.time = this.$moment()._d; // 当前时间 this.time0 =this.$moment().subtrac ...
- MySQL5.7 JSON类型及其相关函数的学习
mysql> CREATE TABLE `json_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `info` json NOT NULL, PR ...
- 5、Storm集成Kafka
1.pom文件依赖 <!--storm相关jar --> <dependency> <groupId>org.apache.storm</groupId> ...
- setInterval中this
今天使用react做钟表,自然用到了setInterval,但是出现this指向不明的问题. <html> <head> <meta charset="UTF- ...
- php登录注册
php 登录注册 注册代码:register.php <style type="text/css"> form{ width:300px; background-col ...