|
|
发表于 2025-1-18 18:06:44
|
显示全部楼层
从错误信息来看,问题源于 `collections` 模块的 `MutableMapping`。在Python 3.3及以上版本中,`collections.MutableMapping` 已经被移动到 `collections.abc.MutableMapping` 中。
你提到已经修改了 `cookies.py` 文件中的 `MutableMapping` 导入,但问题仍然存在。这可能意味着你的修改没有被保存或没有应用到你的Python环境中。以下是解决这个问题的步骤:
- 确保你已经正确保存了修改过的 `cookies.py` 文件。
- 可以尝试卸载并重新安装 `requests` 库。
- 如果问题依旧,可以考虑升级你的 Python 和相关库到最新版本,因为较新的版本很可能已经解决了这个问题。
对于代码中的解析部分,还有几点需要修正:
1. 解析数据时应该使用 `strip()` 列表单元格的文本内容。
2. 列表推导式中缺少中括号。
以下是修正过的代码示例:
- import requests
- from bs4 import BeautifulSoup
- def get_pokemon_info():
- url = '[url=https://wiki.52poke.com/wiki/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%88%97%E8%A1%A8%EF%BC%88%E6%8C%89%E5%85%B3%E9%83%BD%E5%9B%BE%E9%89%B4%E7%BC%96%E5%8F%B7%EF%BC%89']https://wiki.52poke.com/wiki/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%88%97%E8%A1%A8%EF%BC%88%E6%8C%89%E5%85%B3%E9%83%BD%E5%9B%BE%E9%89%B4%E7%BC%96%E5%8F%B7%EF%BC%89'[/url]
- response = requests.get(url)
- soup = BeautifulSoup(response.content, 'html.parser')
- # 找到宝可梦列表的表格
- pokemon_table = soup.find('table', {'class': 'sortable'})
- # 解析表格中的每一行数据
- pokemon_rows = pokemon_table.find_all('tr')
- pokemon_data = {}
- for row in pokemon_rows: # 跳过标题行
- cells = row.find_all('td')
- if not cells:
- continue
- # 获取宝可梦的编号、名字、属性、HP、攻击、防御、速度等信息
- num = cells[0].text.strip()
- name = cells[1].text.strip()
- types = [type.text.strip() for type in cells[2].find_all('a')]
- hp = int(cells[3].text.strip())
- attack = int(cells[4].text.strip())
- defense = int(cells[5].text.strip())
- speed = int(cells[6].text.strip())
- # 存储宝可梦数据
- pokemon_data[num] = {
- '名字': name,
- '属性': types,
- 'HP': hp,
- '攻击': attack,
- '防御': defense,
- '速度': speed
- }
- return pokemon_data
- # 调用函数并打印结果
- pokemon_info = get_pokemon_info()
- for num, info in pokemon_info.items():
- print(f"{num}: {info}")
复制代码
这些修改应该能修复你的代码中的问题。希望这对你有帮助!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|