我需要解析带有“class”的“span”标签中的价目表。 Numbers 有一个特定的设置 (2 400 p),所以我还需要删除空格和“p”字母。 这是我的代码:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("1.html"))
for link in soup.findAll("span", { "class" : "b-sbutton mod_price skin_product size_normal scheme_available" }):
links = link.get_text()
print(links)
links_len = len(links)
int(links_len)
for links_len in links:
a = links[links_len]
a.replace(' ', '')
a.replace('р', '')
print(links)
但是当我尝试运行脚本时出现错误
Traceback (most recent call last):
File "get_data.py", line 9, in <module>
a = links[links_len]
TypeError: string indices must be integers
我在这里错过了什么?
请您参考如下方法:
您混淆了列表、字符串和索引。您可以使用 list comprehension 来制作它:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("1.html"))
links = [link.text.replace(' ', '').replace('p', '')
for link in soup.find_all("span",
{"class": "b-sbutton mod_price skin_product size_normal scheme_available"})]
