2017-02-02 49 views
0

THX! 1.它看起來像它某事的錯誤,但我沒能找到它 2.原來是這樣,但現在如下代碼。塊:Python的「類型錯誤:‘builtin_function_or_method’對象未標化」

from urllib.request import Request, urlopen 
from urllib.error import URLError,HTTPError 
from bs4 import BeautifulSoup 
import re 

print('https://v.qq.com/x/page/h03425k44l2.html\\\\n\\\\https://v.qq.com/x/cover/dn7fdvf2q62wfka/m0345brcwdk.html\\\\n\\\\http://v.qq.com/cover/2/2iqrhqekbtgwp1s.html?vid=c01350046ds') 
web = input('請輸入網址:') 
if re.search(r'vid=',web) : 
    patten =re.compile(r'vid=(.*)') 
    vid=patten.findall(web) 
    vid=vid[0] 

else: 
    newurl = (web.split("/")[-1]) 
    vid =newurl.replace('.html', ' ') 
#從視頻頁面找出vid 

getinfo='http://vv.video.qq.com/getinfo?vids{vid}&otype=xlm&defaultfmt=fhd'.format(vid=vid.strip()) 
def getpage(url): 
    req = Request(url) 
    user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit' 
    req.add_header('User-Agent', user_agent) 
    try: 
     response = urlopen(url) 
    except HTTPError as e: 
     print('The server couldn\\\'t fulfill the request.') 
     print('Error code:', e.code) 
    except URLError as e: 
     print('We failed to reach a server.') 
     print('Reason:', e.reason) 
    html = response.read().decode('utf-8') 
    return(html) 
#打開網頁的函數 

a = getpage(getinfo) 

soup = BeautifulSoup(a, "html.parser") 
for e1 in soup.find_all('url'): 
    ippattent = re.compile(r"((?:(2[0-4]\\\\d)|(25[0-5])|([01]\\\\d\\\\d?))\\\\.){3}(?:(2[0-4]\\\\d)|(255[0-5])|([01]?\\\\d\\\\d?))") 
    if re.search(ippattent,e1.get_text()): 
     ip=(e1.get_text()) 
for e2 in soup.find_all('id'): 
    idpattent = re.compile(r"\\\\d{5}") 
    if re.search(idpattent,e2.get_text()): 
     id=(e2.get_text()) 
filename=vid.strip()+'.p'+id[2:]+'.1.mp4' 
#找到ID和拼接FILENAME 

getkey='http://vv.video.qq.com/getkey?format={id}&otype=xml&vt=150&vid{vid}&ran=0%2E9477521511726081\\\\&charge=0&filename={filename}&platform=11'.format(id=id,vid=vid.strip(),filename=filename) 
#利用getinfo中的信息拼接getkey網址 
b = getpage(getkey) 

key=(re.findall(r'<key>(.*)<\\\\/key>',b)) 

videourl=ip+filename+'?'+'vkey='+key[0] 

print('視頻播放地址 '+videourl) 
#完成了 

我運行它,並得到這個:

Traceback (most recent call last): 
    File "C:\Users\DYZ_TOGA\Desktop\qq.py", line 46, in <module> 
    filename=vid.strip()+'.p'+id[2:]+'.1.mp4' 
TypeError: 'builtin_function_or_method' object is not subscriptable 

我應該怎麼做,我不知道如何改變我的代碼來糾正it.Thank你:)

回答

0

你的問題的根源就在這裏:

if re.search(idpattent,e2.get_text()): 
    id=(e2.get_text()) 

如果這是假的,你永遠不設置id。這意味着id是該名稱的內置函數,該函數獲取任何對象的唯一ID。既然它是一個函數,而不是你期望的字符串,你不能這樣做:

id[2:] 

因此你得到的錯誤。

我的建議是:

  1. 使用不同的變量名;你會得到一個關於它在這種情況下沒有被定義的錯誤,這將使得解決問題更容易

  2. 當你沒有找到ID時,不要繼續腳本;無論如何它不會工作。如果你期望找到它,並不確定爲什麼沒有發生,那麼你應該單獨詢問另一個問題。

0

id是python中的內建函數。看來你正在使用相同的存儲變量。使用關鍵字作爲變量名是個壞習慣。改用一些不同的名字。

0
if re.search(idpattent,e2.get_text()): 
     id=(e2.get_text()) 
filename=vid.strip()+'.p'+id[2:]+'.1.mp4' 

如果上述「如果」是不正確的,ID將不能設置爲字符串值。

默認情況下,id是一個函數是python。所以你不能做id [2:]

因爲python期望id()。

相關問題