【发布时间】:2023-01-26 17:22:44
【问题描述】:
web scrapping python 为什么 find 函数的参数显示错误。
我期待它在标签 <span></span> 中打印数据
例如:
<span>APPLE iPhone 14 (Midnight, 128 GB)</span>
我想从 HTML 代码中提取 APPLE iPhone 14 (Midnight, 128 GB)。 网站链接是:https://www.flipkart.com/apple-iphone-14-midnight-128-gb/p/itm9e6293c322a84 代码:
import requests
from bs4 import BeautifulSoup
url="https://www.flipkart.com/apple-iphone-14-midnight-128-gb/p/itm9e6293c322a84"
r=requests.get(url)
html_content=r.content
soup=BeautifulSoup(html_content,"html.parser").prettify()
name=soup.find("span",{"class":"B_NuCI"})
print(name)
错误:
C:\Users\Asus\PycharmProjects\pythonProject9\venv\Scripts\python.exe C:\Users\Asus\PycharmProjects\pythonProject9\main.py
Traceback (most recent call last):
File "C:\Users\Asus\PycharmProjects\pythonProject9\main.py", line 7, in <module>
name=soup.find("span",{"class":"B_NuCI"})
TypeError: slice indices must be integers or None or have an __index__ method
Process finished with exit code 1
【问题讨论】:
-
帮助我们帮助您 - 请改进您的问题,以便我们可以轻松重现您的问题。花点时间检查 - 如何创建 minimal reproducible example 堆栈溢出图像不应用于文本内容,请参阅 Why should I not upload images of code/data/errors? 了解原因。谢谢
-
请复制您的代码而不是图片,以便我们为您提供帮助。
标签: python html web-scraping web beautifulsoup