Python “TypeError: 'builtin_function_or_method' object is not subscriptable"

Question:

It looks like sth error in it, but i failed to find it.

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)
#完成了

I run it and get this:

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

What should I do? I don’t know how to change my code to correct it.

Asked By: dyz1996dyz

||

Answers:

id is a builtin function in python. it seems you are using the same to store variable. It is bad habit to use keyword as variable name. Use some different name instead.

Answered By: Jay Parikh

The root of your problem is here:

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

If this is false, you never set id. And that means id is the built-in function of that name, which gets the unique ID of any object. Since it’s a function, not the string you expect, you can’t do this:

id[2:]

Hence the error you are getting.

My suggestions are:

  1. Use a different variable name; you would have get an error about it not being defined in this case, which would have made solving the problem easier

  2. When you don’t find the ID, don’t continue the script; it won’t work anyway. If you expected to find it, and are not sure why that’s not happening, that’s a different question you should ask separately.

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

If the above “if” is not true, id will not be set to string value.

By default id is a function is python . So you cannot do id[2:]

because python expects id().

Answered By: Mithilesh Gupta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.