web scrapping python why the parameters of find function is showing error.
I was expecting it to print data in tag <span></span>
for example:
<span>APPLE iPhone 14 (Midnight, 128 GB)</span>
I want to extract APPLE iPhone 14 (Midnight, 128 GB) from the HTML code. link of the website is: https://www.flipkart.com/apple-iphone-14-midnight-128-gb/p/itm9e6293c322a84 code:
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)
error:
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
Remove the .prettify()
, because it will convert your BeautifulSoup
object into a string, so you no longer be able to access the info the way you try to do.
The goal of prettify() is to help you visually understand the structure of the documents you work with.
If you just wanna see pretty structure use it only this way print(soup.prettify())
not on BeautifulSoup(...).prettify()
import requests
soup = BeautifulSoup(
requests.get('https://www.flipkart.com/apple-iphone-14-midnight-128-gb/p/itm9e6293c322a84').text
)
soup.find('span', class_='B_NuCI').text
APPLE iPhone 14 (Midnight, 128 GB)