代码错误
本帖最后由 Vastsea 于 2020-5-13 08:44 编辑import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ip = ['10.0.3.12','10.0.55.20','10.0.3.18','10.0.70.18','10.0.3.15','10.0.70.24']
for i in ip:
url = "http://" + i + "/web/guest/cn/websys/status/getUnificationCounter.cgi"
req = requests.get(url)
html = req.text
bs = BeautifulSoup(html,"html.parser")
trs = bs.select('tr.staticProp').get_text()
count = trs
ws.title
ws['A1'] = "IP地址"
ws['B1'] = "总数"
for n in count
ws.append()
ws.save(""c:\\New folder\\11.xlsx"")
提示如下错误
File "<ipython-input-4-58c9043a6f9c>", line 19
for n in count
^
SyntaxError: invalid syntax “SyntaxError: invalid syntax” 的意思就是 语法错误;
经过查询解决了这个问题,所以总结一个这个问题的解决方法:
1.版本问题:
因为python2和python3是不兼容的,所以一些可以在python2上运行的代码不一定可以在python3上运行;可以尝试更换版本;
2.路径问题:
记得仔细查看自己的路径是否正确;
3.粗心问题:
忘记在 if , elif , else , for , while , class ,def 声明末尾添加 冒号( : );
误将 = 当成 == 使用;
4.安装第三方模块时:
在安装第三方模块时也有可能出现“SyntaxError: invalid syntax”这个问题,这时需要检查一些是否是在cmd窗口下安装,同时,要到python的安装目录里面,找到pip所在的目录里面进行安装; 第19行“for n in count”,没加冒号 非常感谢!又出现新的错误提示。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-abafa52f0b18> in <module>
19 for n in count:
20 ws.append()
---> 21 ws.save("c:\\New folder\\11.xlsx")
AttributeError: 'Worksheet' object has no attribute 'save' AttributeError:尝试访问未知的对象属性
'Worksheet' object has no attribute 'save':‘工作表’对象没有属性
问题出在这里:
for n in count:
ws.append()
ws.save(""c:\\New folder\\11.xlsx"")
非常感谢,将WS改为wb.save就可以了。 import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ip = ['10.0.3.12','10.0.55.20','10.0.3.18','10.0.70.18','10.0.3.15','10.0.70.24']
for i in ip:
url = "http://" + i + "/web/guest/cn/websys/status/getUnificationCounter.cgi"
req = requests.get(url)
html = req.text
bs = BeautifulSoup(html,"html.parser")
trs = bs.select('tr.staticProp').get_text()
count = trs
ws.title
ws['A1'] = "IP地址"
ws['B1'] = "总数"
for n in count:
ws.append()
ws.save(""c:\\New folder\\11.xlsx"")
页:
[1]