代码如下,一直报错 —— UnboundLocalError: local variable 'dian' referenced before assignment
查了一下好像是说全局变量应用的问题,但是我好像并没有声明全局变量呀。这是哪里出差了?求解答
import requests
import re
def gethtml(url):
try:
headers={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"}
req=requests.get(url,headers=headers)
req.raise_for_statues()
req.encoding=req.apparent_encoding
except:
return "error"
return req.text
def getinfo(html):
obj = re.compile(r'.*?.*?(?P<name>.*?)'
r'.*?
(?P<year>.*?) .*?(?P<score>.*?)'
r'.*?(?P<number>.*?)人评价', re.S)
i = obj.finditer(html)
for it in i:
dian=it.groupdict()
dian['year'] = dian['year'].strip()
return dian
def main():
url ='https://movie.douban.com/top250'
html=gethtml(url)
getinfo(html)
if __name__ == '__main__':
main()