Мои данные очистки Python пусты, не знаю, что пошло не так с моим кодомPython

Программы на Python
Anonymous
Мои данные очистки Python пусты, не знаю, что пошло не так с моим кодом

Сообщение Anonymous »

Мой результат пуст, не знаю, что пошло не так с моим кодом:
Вот мой код:
#попытка получить данные с этого сайта https://www.watsons.com.sg/health/c/210 ... rentPage=1

Код: Выделить всё

import requests
from bs4 import BeautifulSoup
import pandas as pd

# Setting display options for pandas
pd.options.display.width = 1000
pd.options.display.max_rows = 1000
Создание пользовательского агента

Код: Выделить всё

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

# Create lists
items = []
prices = []

# Function to scrape a page
def scrape_page(page):
try:
url = f'https://www.watsons.com.sg/health/c/2100000?currentPage={page}'
response = requests.get(url, headers=HEADERS)
response.raise_for_status()  # Check for request errors

soup = BeautifulSoup(response.text, 'html.parser')

# Find product items
product_items = soup.find_all('e2-product-tile', class_='ng-star-inserted hasPromotion-2')
print(soup)

for item in product_items:
# Extract product name
name = item.find('h2', class_='productName').get_text(strip=True)
items.append(name)

# Extract product price
price = item.find('div', class_='formatted-value ng-star-inserted').get_text(strip=True)
prices.append(price)

except requests.RequestException as e:
print(f"An error occurred: {e}")
В качестве примера выполните парсинг первых 5 страниц

Код: Выделить всё

for page in range(1, 2):
scrape_page(page)
Создайте фрейм данных

Код: Выделить всё

df = pd.DataFrame({'Item': items, 'Price': prices})
Отображение фрейма данных

Код: Выделить всё

print(df)
#Выше приведен мой код

Подробнее здесь: https://stackoverflow.com/questions/787 ... th-my-code

Вернуться в «Python»