Python Beginner - IndexError: list index out of range error in beautiful soup

Asked 2 years ago, Updated 2 years ago, 120 views

If you run the following during web-scaping practice, you will get an error with IndexError: list index out of range.
Why?

import requests
import re
from bs4 import BeautifulSoup

url = "https://www.coupang.com/np/search?q=%EB%85%B8%ED%8A%B8%EB%B6%81&channel=user&component=&eventCategory=SRP&trcid=&traid=&sorter=scoreDesc&minPrice=&maxPrice=&priceRange=&filterType=&listSize=36&filter=&isPriceRange=false&brand=&offerCondition=&rating=0&page=1&rocketAll=false&searchIndexingToken=1=4&backgroundColor="
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"}
res = requests.get(url, headers=headers)
res.raise_for_status()
soup = BeautifulSoup(res.text, "lxml")

items = soup.find_all("li", attrs={"class":re.compile("^search=product")})
print(items[0].find("div", attrs={"class":"name"}).get_text())

python beautifulsoup

2022-09-20 19:14

1 Answers

https://hashcode.co.kr/questions/9679/%ED%8C%8C%EC%9D%B4%EC%8D%AC-list-index-out-of-range-%EC%97%90%EB%9F%AC

Link the previously posted answer for the list index out of range error.


2022-09-20 19:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.