[英]python, urllib2 weird error?
朋友們。
我正在嘗試重寫我的小工具之一。 基本上,它從用戶那里獲得輸入,如果該輸入不包含“基本URL”,則函數會將該輸入構造為有效的URL,以供程序的其他部分使用。
如果是我寫的,那么該程序只接受有效的URL作為輸入,它將起作用; 但是,如果我傳遞一個字符串並對其進行構造,則urllib2.urlopen()將失敗,而且我也不知道為什么,因為返回的值與str值完全相同...
import urllib2
import re
class XunLeiKuaiChuan:
kuaichuanBaseAddress = 'http://kuaichuan.xunlei.com/d/'
regexQuery = 'file_name=\"(.*?)\"\sfile_url=\"(.*?)\sfile_size=\"(.*?)\"'
agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)'
def buildLink(self, aLink):
if aLink == '':
return
if 'xunlei.com' not in aLink:
aLink = self.kuaichuanBaseAddress + aLink
return aLink
def decodeLink(self, url):
url = self.buildLink(url) #it will return correct url with the value provided.
print 'in decodeLink ' + url
urlReq = urllib2.Request(url)
urlReq.add_header('User-agent', self.agent)
pageContent = urllib2.urlopen(urlReq).read()
realLinks = re.findall(self.regexQuery, pageContent)
return realLinks
test = XunLeiKuaiChuan()
link='y7L1AwKuOwDeCClS528'
link2 = 'http://kuai.xunlei.com/d/y7L1AwKuOwDeCClS528'
s = test.decodeLink(link2)
print s
當我用link2調用它時,它將按預期運行。 會在使用“鏈接”時失敗,有人告訴我我在這里想念什么? 我的“舊版本”只能接受完整的網址,但是這種未知的行為在這里使我喪命……謝謝。
順便說一句,如果具有完整的URL,它返回一個空列表,只需打開URL,然后在頁面上輸入catcha。 他們這樣做是為了防止某種“攻擊”。
沒關系,我在代碼中輸入了錯誤的主機名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.