Vessel_IOU = [], ACC = [], RECALL = [], PRECISION = [], DICE = [], AUC = [] ValueError: not enough values to unpack (expected 2, got 0)错在哪里
时间: 2023-08-18 07:54:32 浏览: 38
这个错误提示意味着你在尝试解包的时候,期待得到2个值,但实际上只有0个值。解包操作通常是将一个可迭代对象的值分配给多个变量。
在这种情况下,你需要检查一下你尝试解包的对象是否包含足够的值。例如,确保Vessel_IOU、ACC、RECALL等列表中有至少两个元素。如果这些列表是空的,那么解包操作就会失败。
另外,还需要确保你使用的解包语法正确,例如正确使用了等号和方括号。请检查一下这些方面,并确保你的代码符合预期。
相关问题
你可以帮我把以下元素的索引更新到这里面么soup = BeautifulSoup(response.text, "html.parser") for row in soup.select(".table-row"): departure_port = row.select_one(".departure-port").text.strip() departure_time = row.select_one(".departure-time").text.strip() arrival_port = row.select_one(".arrival-port").text.strip() arrival_time = row.select_one(".arrival-time").text.strip() vessel_name = row.select_one(".vessel-name").text.strip()
当然可以,以下是您所需要的所有元素的索引:
```python
soup = BeautifulSoup(response.text, "html.parser")
for row in soup.select(".table-row"):
departure_port = row.select_one(".departure-port").text.strip()
departure_time = row.select_one(".departure-time").text.strip()
arrival_port = row.select_one(".arrival-port").text.strip()
arrival_time = row.select_one(".arrival-time").text.strip()
vessel_name = row.select_one(".vessel-name").text.strip()
terminal = row.select_one('div[class="mds-text--medium-bold"]').text.strip()
voyage_number = row.select('td')[5].text.strip()
transit_time = row.select('td')[6].text.strip()
service_name = row.select('td')[7].text.strip()
service_type = row.select('td')[8].text.strip()
price = row.select('td')[9].text.strip()
print(departure_port, departure_time, arrival_port, arrival_time, vessel_name, terminal, voyage_number, transit_time, service_name, service_type, price)
```
这里增加了以下元素的索引:
- `terminal`:使用了之前提到的 CSS 选择器来查找具有 `class="mds-text--medium-bold"` 属性的 `div` 元素;
- `voyage_number`:使用了 `row.select('td')[5]` 来获取行中第 6 个 `td` 元素,并使用 `text` 属性来获取元素的文本内容;
- `transit_time`:使用了 `row.select('td')[6]` 来获取行中第 7 个 `td` 元素,并使用 `text` 属性来获取元素的文本内容;
- `service_name`:使用了 `row.select('td')[7]` 来获取行中第 8 个 `td` 元素,并使用 `text` 属性来获取元素的文本内容;
- `service_type`:使用了 `row.select('td')[8]` 来获取行中第 9 个 `td` 元素,并使用 `text` 属性来获取元素的文本内容;
- `price`:使用了 `row.select('td')[9]` 来获取行中第 10 个 `td` 元素,并使用 `text` 属性来获取元素的文本内容。
这些元素的索引可以通过观察网页的 HTML 代码和属性来确定。
for row in soup.select(".table-row"): departure_port = soup.select_one("div:contains('Shanghai East Container Terminal')").text.strip() departure_time = soup.select_one("div[data-test='departure-date']").text.strip() arrival_port = soup.select_one("div:contains('PSA Singapore Terminal')").text.strip() arrival_time = soup.select_one("div[data-test='arrival-date']").text.strip() vessel_name = soup.select_one("span:contains('NIMTOFTE MAERSK')").text.strip()这一步是什么意思
这一步的作用是使用CSS选择器来从BeautifulSoup对象`soup`中提取所需的数据,并将其存储在变量中。
首先,`soup.select(".table-row")`使用CSS选择器`.table-row`来匹配所有的class为`table-row`的元素,这些元素包含了船期信息的各个字段。
然后,对于每个匹配到的元素,使用相应的CSS选择器来取出所需的字段值,例如:
- `departure_port = soup.select_one("div:contains('Shanghai East Container Terminal')").text.strip()`: 使用CSS选择器`div:contains('Shanghai East Container Terminal')`匹配到第一个包含"Shanghai East Container Terminal"文本的div元素,并将其文本内容去除空格和换行符后存储在`departure_port`变量中。
- `departure_time = soup.select_one("div[data-test='departure-date']").text.strip()`: 使用CSS选择器`div[data-test='departure-date']`匹配到第一个包含`data-test`属性值为`departure-date`的div元素,并将其文本内容去除空格和换行符后存储在`departure_time`变量中。
- `arrival_port = soup.select_one("div:contains('PSA Singapore Terminal')").text.strip()`: 使用CSS选择器`div:contains('PSA Singapore Terminal')`匹配到第一个包含"PSA Singapore Terminal"文本的div元素,并将其文本内容去除空格和换行符后存储在`arrival_port`变量中。
- `arrival_time = soup.select_one("div[data-test='arrival-date']").text.strip()`: 使用CSS选择器`div[data-test='arrival-date']`匹配到第一个包含`data-test`属性值为`arrival-date`的div元素,并将其文本内容去除空格和换行符后存储在`arrival_time`变量中。
- `vessel_name = soup.select_one("span:contains('NIMTOFTE MAERSK')").text.strip()`: 使用CSS选择器`span:contains('NIMTOFTE MAERSK')`匹配到第一个包含"NIMTOFTE MAERSK"文本的span元素,并将其文本内容去除空格和换行符后存储在`vessel_name`变量中。
最后,将上述变量的值存储在一个字典中,表示一条船期信息。这个字典可以用于后续的数据处理和存储。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)