0x00 错误

Traceback (most recent call last):
File "/Users/apple/Desktop/Me/My/Python3/MyCode/test.py", line 43, in <module>
test()
File "/Users/apple/Desktop/Me/My/Python3/MyCode/test.py", line 13, in test
s = str(count)
UnboundLocalError: local variable 'str' referenced before assignment

0x01 代码

代码:

import requests
from bs4 import BeautifulSoup as bs

def test():

count = 0
while True:
count += 1
s = str(count)
print('第 ' + s + ' 次')
break

print('begin...')

headers = {'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Mobile Safari/537.36'}
url = 'https://www.xxx.xxxx'
r = requests.get(url, headers = headers)
soup = bs(r.text, 'lxml')

kk = soup.find(class_='xxxxx')
str = kk.text
arr = str.split('个')

if len(arr) == 2:
count = arr[0]
if len(count) > 0:
if int(count) > 10:
print(str)


if __name__ == '__main__':
test()

数字转字符串不就是这样么?

count = 0
str(count)

全局搜索了一下代码
原来在后面定义了一个 ​​​str​​​ 变量
这个变量,覆盖了系统的类 ​​​str​​​ 所以,导致了
​UnboundLocalError: local variable 'str' referenced before assignment​


0x02 办法

既然是命名覆盖,那就换个名字好了~
要注意的一件事情是:
​​​不要跟系统的类重名了!​​ 😂😂


小笔记应用