【发布时间】:2021-04-07 08:18:17
【问题描述】:
我正在尝试使用 Selenium 和 Python 对 Aliexpress 进行网络抓取。我是按照 youtube 教程来做的,我已经按照每个步骤进行操作,但我似乎无法让它工作。
我尝试使用请求,BeautifulSoup 也是如此。但似乎 Aliexpress 在其产品列表中使用了惰性加载器。我尝试使用窗口滚动脚本,但没有奏效。在我亲自滚动之前,内容似乎不会加载。
这是我目前拥有的代码。它不会在输出中返回任何内容。我认为那是因为它正在尝试浏览所有产品列表,但找不到任何产品列表,因为它没有加载...
任何建议/帮助将不胜感激,对于错误的格式和错误的代码,我们深表歉意。
谢谢!
"""
To do
HOT PRODUCT FINDER Enter: Keyword, to generate a url
Product Name
Product Image
Product Link
Sales Number
Price
Create an excel file that contains these data
Sort the list by top selling orders
Develop an algorithm for the velocity of the product (total sales increased / time?)
Scrape site every day """
import csv
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import requests
import lxml
#Starting Up the web driver
driver = webdriver.Chrome()
# grab Keywords
search_term = input('Keywords: ')
# url generator
def get_url(search_term):
"""Generate a url link using search term provided"""
url_template = 'https://www.aliexpress.com/wholesale?trafficChannel=main&d=y&CatId=0&SearchText={}<ype=wholesale&SortType=default&g=n'
search_term = search_term.replace(" ", "+")
return url_template.format(search_term)
url = get_url('search_term')
driver.get(url)
#scrolling down to the end of the page
time.sleep(2)
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
#Extracting the Collection
r = requests.get(url)
soup = BeautifulSoup(r.content,'lxml')
productlist = soup.find_all('div', class_='list product-card')
print(productlist)
【问题讨论】:
标签: python selenium beautifulsoup