简体   繁体   中英

Python IndexError: list index out of range. Can someone help me?

Can someone help me to solve my problem in this code? CODE:

from bs4 import BeautifulSoup

import requests

headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 
Gecko) Chrome/83.0.4103.61 Safari/537.36"}

url = "https://www.amazon.com/RUNMUS-Surround-Canceling-Compatible-Controller/dp/B07GRM747Y"

resp = requests.get(url, headers=headers)

s = BeautifulSoup(resp.content, features='lxml')

product_title = s.select("#productTitle")[0].get_text().strip()

print(product_title)

If you try to print what you get as response, you will not encounter related errors.

import requests

headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"}

url = "https://www.amazon.com/RUNMUS-Surround-Canceling-Compatible-Controller/dp/B07GRM747Y"

resp = requests.get(url, headers=headers)
print(resp.content)

The output you are getting from this request:

b'<!--\n        To discuss automated access to Amazon data please contact api-services-support@amazon.com.\n        For information about migrating to our APIs refer to our Marketplace APIs...

The site you are sending requests is not allowing you to access content with provided headers. So your s.select("#productTitle") creates empty list therefore you are getting an index error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM