2015-07-28 187 views
1
# -*- coding: UTF-8 -*- 

import urllib.request 
import re 
import os 

os.system("cls") 

url=input("Url Link : ") 

if(url[0:8]=="https://"): 
    url=url[:4]+url[5:] 

if(url[0:7]!="http://"): 
    url="http://"+url 
try : 
    try : 
     value=urllib.request.urlopen(url,timeout=60).read().decode('cp949') 
    except UnicodeDecodeError : 
     value=urllib.request.urlopen(url,timeout=60).read().decode('UTF8') 
    par='<title>(.+?)</title>' 

    result=re.findall(par,value) 
    print(result) 

except ConnectionResetError as e: 
    print(e) 

TimeoutError消失。但是出现ConnectionResetError。这个错误是什么?它是服务器问题吗?所以它不能解决我?“ConnectionResetError”我该怎么办?

+0

这将是很好,如果你可以添加完整回溯 – The6thSense

+0

主页是[这里](http://jakjeon.icems.kr/main.do) –

+0

@VigneshKalai什么是回溯?我能怎么做? –

回答

1

포기하지마세요!不要放弃!

某些网站需要特定的HTTP标头,在这种情况下,需要User-agent。所以你需要在你的请求中设置这个头。

改变你这样的请求(17 - 20行代码的)

# Make request object 
request = urllib.request.Request(url, headers={"User-agent": "Python urllib test"}) 

# Open url using request object 
response = urllib.request.urlopen(request, timeout=60) 

# read response 
data = response.read() 

# decode your value 
try: 
    value = data.decode('CP949') 
except UnicodeDecodeError: 
    value = data.decode('UTF-8') 

您可以更改"Python urllib test"到你想要的任何东西。出于统计目的,几乎每个服务器都使用User-agent

最后,考虑使用appropritate空格,空行,注释使您的代码更具可读性。这对你有好处。


更多阅读:

+0

谢谢你这个问题已经完成。你是韩国人吗? –

+0

“python urllib测试”不受影响? –

+0

是的。您可以将其更改为不为空的任何字符串。是。我是韩国人:) – mrorno