0

I want to get the product title from an amazon url-https://www.amazon.in/BATA-Fenny-Sneakers-7-India-8219990/dp/B07P8PMS25/ref=asc_df_B07P8PMS25/?tag=googleshopdes-21&linkCode=df0&hvadid=397006879402&hvpos=&hvnetw=g&hvrand=2284563689588211961&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1007824&hvtargid=pla-837374864561&psc=1&ext_vrnc=hi. I tried this code

from bs4 import *
import requests
head={'user-agent':'betbargain/android-7.0/0.0.5'}
amaurl=input('Enter amazon url')
amazinfo=requests.get(amaurl,headers=head)
amasoup=BeautifulSoup(amazinfo.text,'lxml')
amatit=amasoup.find("span", attrs={"id":'productTitle'}).string.strip()
print(amatit)

But when I input the url it says-

Traceback (most recent call last):
  File "c:/Users/rauna/Desktop/bb.py", line 7, in <module>
    amatit=amasoup.find("span", attrs={"id":'productTitle'}).string.strip()
AttributeError: 'NoneType' object has no attribute 'string'

I have no idea why this has happened. Please tell me where I am wrong. Thanks in advance.

0

3 Answers 3

1

Change the search to <h1> with id="title":

from bs4 import *
import requests
head={'user-agent':'betbargain/android-7.0/0.0.5'}
# amaurl=input('Enter amazon url')
amaurl = 'https://www.amazon.in/BATA-Fenny-Sneakers-7-India-8219990/dp/B07P8PMS25/ref=asc_df_B07P8PMS25/?tag=googleshopdes-21&linkCode=df0&hvadid=397006879402&hvpos=&hvnetw=g&hvrand=2284563689588211961&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1007824&hvtargid=pla-837374864561&psc=1&ext_vrnc=hi'
amazinfo=requests.get(amaurl,headers=head)
amasoup=BeautifulSoup(amazinfo.text,'lxml')
amatit=amasoup.find("h1", attrs={"id":'title'}).get_text(strip=True)  # <-- change to <h1 id="title">
print(amatit)

Prints:

BATA Men's Fenny Sneakers
Sign up to request clarification or add additional context in comments.

1 Comment

gist.github.com/Raunanza/… This code I have copied from a website. It's working but I have no idea why and how. Can you please explain.
1

If find does not find anything, it will return None. Have a look at the documentation. You have to check if find has found anything before doing something with the result.

Also instead of .string, I think you want str().

Try this instead:

from bs4 import *
import requests

head = {'user-agent':'betbargain/android-7.0/0.0.5'}
amaurl = input('Enter amazon url')
amazinfo = requests.get(amaurl,headers=head)
amasoup = BeautifulSoup(amazinfo.text,'lxml')

findResult = amasoup.find("span", attrs={"id":'productTitle'})
amatit = ""
if (findResult != None): # Added a check for findResult being None
    amatit = str(findResult).strip() # Changed .string to str()
print(amatit)

Comments

0

try this:

from bs4 import *
import requests
head={'user-agent':'betbargain/android-7.0/0.0.5'}
amaurl=input('Enter amazon url')
amazinfo=requests.get(amaurl,headers=head)
amasoup=BeautifulSoup(amazinfo.text,'lxml')
amatit=str(amasoup.find("span", attrs={"id":'productTitle'})).strip()
print(amatit)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.