相关疑难解决方法(0)

Python - 如何验证python中的url?(畸形与否)

我url来自用户,我必须回复提取的HTML.

如何检查URL是否格式错误?

例如 :

url='google'  // Malformed
url='google.com'  // Malformed
url='http://google.com'  // Valid
url='http://google'   // Malformed
Run Code Online (Sandbox Code Playgroud)

我们怎样才能做到这一点?

python url malformedurlexception

93
推荐指数
9
解决办法
13万
查看次数

Python中的异步异常处理

我有以下代码使用asyncio和aiohttp发出异步HTTP请求.

import sys
import asyncio
import aiohttp

@asyncio.coroutine
def get(url):
    try:
        print('GET %s' % url)
        resp = yield from aiohttp.request('GET', url)
    except Exception as e:
        raise Exception("%s has error '%s'" % (url, e))
    else:
        if resp.status >= 400:
            raise Exception("%s has error '%s: %s'" % (url, resp.status, resp.reason))

    return (yield from resp.text())

@asyncio.coroutine
def fill_data(run):
    url = 'http://www.google.com/%s' % run['name']
    run['data'] = yield from get(url)

def get_runs():
    runs = [ {'name': 'one'}, {'name': 'two'} ]
    loop …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio

38
推荐指数
2
解决办法
3万
查看次数