Я соскребаю страницу Amazon, используя Python и сохраняю результат в файл CSV. Этот код работает хорошо, но проблема в том, что я получаю некоторые имена продуктов без первого слова.
Так, например, здесь я получаю только:
«Шуко-стекдоз, eu-standard 1 fach unterputz mit 2,5d изогнутый платте, Wandsteckdose weiã 86 * 86mm», но он должен быть «o schuk schuk o schuk o schuk ro Unterputz MIT 2,5d Изогнутый Glas Platte, WandSteckDose weiã 86 * 86 мм "
[Ввод изображение описание здесь] [1] < /p>
Вот мой код: < /p>
import requests
from bs4 import BeautifulSoup
import pandas as pd
from time import sleep
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
'Accept-Language': 'de-DE, de;q=0.5'
}
search_query = 'steckdose'.replace(' ', '+')
base_url = 'https://www.amazon.de/s?k={0}'.format(search_query)
items = []
for i in range(1, 2):
print('Processing {0}...'.format(base_url + '&page={0}'.format(i)))
response= requests.get(base_url + '&page={0}'.format(i), headers = headers)
if response.status_code !=200:
print(f"Error: {response.status_code}")
continue
soup = BeautifulSoup(response.content, 'html.parser')
results = soup.find_all('div', {'data-component-type': 's-search-result'})
if not results:
print('No results found')
continue
for result in results:
try:
# Find the tag first
link = result.find('a', class_='a-link-normal s-line-clamp-4 s-link-style a-text-normal')
if link:
# Extract product name from the tag inside
product_name = link.find('h2').find('span').text.strip() # Get text from
product_url = 'https://www.amazon.de' + link['href']
items.append([product_name, product_url])
except AttributeError:
continue
sleep(1.5)
df = pd.DataFrame(items, columns=['product', 'product url'])
df.to_csv('{0}.csv'.format(search_query), index = False)
Подробнее здесь: https://stackoverflow.com/questions/796 ... ith-python
Amazon Web Scraping с Python [закрыто] ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение