2017-02-02 3 views
0

THX !! 1.それはそれでs番目のエラーのように見えますが、私はそれを見つけることができなかった 2.だから、これですが、今のコードを次の。ブロック:Pythonの「例外TypeError: 『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()) 

これがfalseの場合、あなたは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()を期待しているためです。

関連する問題