line 9, in <module> html=re.findall('<li><a href="/song\?id=(\d+)">(.*?)</a>'),response.text TypeError: findall() missing 1 required positional argument: 'string'
时间: 2024-03-04 17:54:05 浏览: 25
这个错误是因为在使用 `re.findall()` 函数时,缺少了需要匹配的字符串参数。可以检查一下代码,看看是否在调用 `re.findall()` 函数时,没有将需要匹配的字符串作为第一个参数传入函数中。可能是因为 `response.text` 返回的是空字符串,导致了这个错误。可以先打印一下 `response.text` 看看是否为空。
相关问题
Traceback (most recent call last): File "C:\Users\test\PycharmProjects\pythonProject\DIDI_test1.py", line 39, in <module> content = re.findall(r'<a href="(.*?)">', match[5])[0] if type in ['音频', '图片'] else match[5] ~~~~~^^^ IndexError: tuple index out of range
这个错误通常发生在`re.findall`函数的返回结果为空列表时,而你试图问列表的第一个元素。
在你的代码中,这个错误是由于正则表达式模式`r'<a href="(.*?)">'`在匹配`match[5]`时没有找到任何结果,导致返回的匹配结果列表为空。
为了避免这个错误,你可以在访问列表元素之前先进行检查,确保列表不为空。可以使用`len()`函数来检查列表的长度。
修改代码如下:
```python
# 遍历匹配结果并输出
for match in matches:
talkid = match[0]
time = match[1].strip()
sender = match[2].strip()
receiver = match[3].strip()
type = match[4].strip()
# 检查列表是否为空
if len(match) > 5:
content = re.findall(r'<a href="(.*?)">', match[5])[0] if type in ['音频', '图片'] else match[5]
else:
content = ''
# 写入CSV文件
csv_writer.writerow([talkid, time, sender, receiver, content])
```
通过使用`len(match)`来检查列表`match`的长度,如果长度大于5,则继续提取内容;否则将`content`设置为空字符串。
这样修改后,即使正则表达式模式没有找到匹配结果,也能避免出现索引超出范围的错误。
File "D:/报分表.py", line 3, in <module> data = xlrd.open_workbook("总体漳州艺术实验学校_总分报表")
这个错误提示是在打开 Excel 文件时出现的。可能是因为指定的文件名有误或者文件不存在导致的。请确认 Excel 文件的路径和名称是否正确,并且确保该文件存在于指定路径下。同时,你需要安装 `xlrd` 模块来读取 Excel 文件。你可以通过在命令行中运行 `pip install xlrd` 命令来安装该模块。