Is féidir le faisnéis a nochtadh ó ríomh-mhiondíoltóirí ar nós aliexpress a bheith an-tairbheach do bhailiú faisnéise táirge, do luaineachtaí praghais monatóireachta, athbhreithnithe a bhailiú, agus mar sin de. San Airteagal seo, déanfaimid iniúchadh ar an bpróiseas chun faisnéis a fháil faoi tháirgí (mar ainm, praghas, rátáil, etc.) agus déanfaimid athbhreithniú ar athbhreithnithe scríobtha táirgí. Léireoimid freisin conas an dinimic scraper a dhéanamh trí URL an táirge a rith, an ID táirge a aisghabháil go huathoibríoch, agus na sonraí a shábháil i gcomhad CSV.
Bainfidh an rang teagaisc seo úsáid as an drámadóir chun ábhar dinimiciúil agus iarratais a dhéanamh chun sonraí athbhreithnithe a fháil. Cinnteoimid freisin go bhfuil an scraper eiticiúil agus go gcloíonn sé le dea -chleachtais.
Sula dtosaímid, cinntigh go bhfuil na leabharlanna Python seo a leanas suiteáilte agat:
Is féidir leat na pacáistí seo a shuiteáil trí na horduithe seo a leanas a reáchtáil:
# Tar chun Playwright
pip install playwright
# Tar chun Requests
pip install requests
# Suiteáil LXML chun HTML a pharsáil
pip install lxml
# Suiteáil pandas le haghaidh cúbláil agus coigilt sonraí
pip install pandas
Tar éis duit drámadóir a shuiteáil, beidh ort na binaries brabhsálaí riachtanacha a shuiteáil freisin:
playwright install
Íoslódáil agus bunóidh sé seo an brabhsálaí riachtanach don drámadóir chun feidhmiú i gceart.
Tá leathanaigh táirge Aliexpress dinimiciúil, rud a chiallaíonn go luchtaíonn siad ábhar trí JavaScript. Chun é seo a láimhseáil, bainfimid úsáid as drámadóir, leabharlann Python a ligeann duit brabhsálaí gan dídean a rialú agus idirghníomhú le hábhar dinimiciúil.
Seo an chaoi ar féidir leat iarratas a sheoladh agus nascleanúint a dhéanamh chuig an leathanach táirge:
from playwright.async_api import async_playwright
async def get_page_content(url):
async with async_playwright() as p:
# Seoladh an brabhsálaí le seachvótálaí más gá (is féidir é a bhaint mura bhfuil sé ag úsáid seachvótálaí)
browser = await p.firefox.launch(
headless=False,
proxy={"server": '', 'username': '', 'password': ''}
)
page = await browser.new_page()
await page.goto(url, timeout=60000)
# Sliocht Ábhar Leathanach
content = await page.content()
await browser.close()
return content
# URL sampla
url = 'https://www.aliexpress.com/item/3256805354456256.html'
Nuair a bhíonn an t -ábhar leathanaigh againn, is féidir linn na sonraí táirge a bhaint as ceisteanna LXML agus XPATH. Baileoimid sonraí cosúil le teideal an táirge, praghas, rátáil, líon na n -athbhreithnithe, agus líon na n -ítimí a díoladh.
from lxml.html import fromstring
def extract_product_data(content):
parser = fromstring(content)
# Sliocht Sonraí Táirgí ag baint úsáide as XPath
title = parser.xpath('//h1[@data-pl="product-title"]/text()')[0].strip()
price = parser.xpath('//div[@class="price--current--I3Zeidd product-price-current"]/span/text()')[0].strip()
rating = ' '.join(parser.xpath('//a[@class="reviewer--rating--xrWWFzx"]/strong/text()')).strip()
total_reviews = parser.xpath('//a[@class="reviewer--reviews--cx7Zs_V"]/text()')[0].strip()
sold_count = parser.xpath('//span[@class="reviewer--sold--ytPeoEy"]/text()')[0].strip()
product_data = {
'title': title,
'price': price,
'rating': rating,
'total_reviews': total_reviews,
'sold_count': sold_count
}
return product_data
Baineann an cód seo úsáid as XPath chun sonraí ábhartha an táirge a bhaint as ábhar HTML an leathanaigh.
Tá críochphointe API ar leith ag Aliexpress chun athbhreithnithe táirgí a fháil. Is féidir leat an ID táirge a bhaint as an URL go dinimiciúil agus é a úsáid chun athbhreithnithe a fháil trí iarratais. Sa fheidhm seo:
import requests
def extract_product_id(url):
# Sliocht Aitheantas Táirge ón URL
product_id = url.split('/')[-1].split('.')[0]
return product_id
def scrape_reviews(product_id, page_num=1, page_size=10):
headers = {
'accept': 'application/json, text/plain, */*',
'accept-language': 'en-IN,en;q=0.9',
'referer': f'https://www.aliexpress.com/item/{product_id}.html',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
}
params = {
'productId': product_id,
'lang': 'en_US',
'country': 'US',
'page': str(page_num),
'pageSize': str(page_size),
'filter': 'all',
'sort': 'complex_default',
}
response = requests.get('https://feedback.aliexpress.com/pc/searchEvaluation.do', params=params, headers=headers)
reviews = response.json()['data']['evaViewList']
# Téacs Athbhreithnithe Sliocht Amháin
review_texts = [review['buyerFeedback'] for review in reviews]
return review_texts
Tar éis sonraí agus athbhreithnithe an táirge a scríobadh, sábhálaimid na sonraí seo i gcomhad CSV ag baint úsáide as leabharlann Pandas.
import pandas as pd
def save_to_csv(product_data, reviews, product_id):
# Sábháil sonraí táirge chuig CSV
df_product = pd.DataFrame([product_data])
df_product.to_csv(f'product_{product_id}_data.csv', index=False)
# Sábháil athbhreithnithe ar CSV
df_reviews = pd.DataFrame({'reviews': reviews})
df_reviews.to_csv(f'product_{product_id}_reviews.csv', index=False)
print(f"Data saved for product {product_id}.")
Sábháltar sonraí agus athbhreithnithe an táirge i gcomhaid CSV ar leith leis an ID táirge atá san áireamh san ainm comhaid le haghaidh aitheantais éasca.
Seo an chaoi a n -oibríonn an sreabhadh oibre dinimiciúil iomlán:
# Sliocht Aitheantas Táirge ón URL
def extract_product_id(url):
return url.split('/')[-1].split('.')[0]
from playwright.async_api import async_playwright
from lxml.html import fromstring
import requests
import pandas as pd
# Faigh Ábhar Leathanach ag baint úsáide as drámadóir
async def get_page_content(url):
async with async_playwright() as p:
browser = await p.firefox.launch(
headless=False,
proxy={"server": '', 'username': '', 'password': ''}
)
page = await browser.new_page()
await page.goto(url, timeout=60000)
content = await page.content()
await browser.close()
return content
# Sonraí Táirgí a Shliocht
def extract_product_data(content):
parser = fromstring(content)
title = parser.xpath('//h1[@data-pl="product-title"]/text()')[0].strip()
price = parser.xpath('//div[@class="price--current--I3Zeidd product-price-current"]/span/text()')[0].strip()
rating = ' '.join(parser.xpath('//a[@class="reviewer--rating--xrWWFzx"]/strong/text()')).strip()
total_reviews = parser.xpath('//a[@class="reviewer--reviews--cx7Zs_V"]/text()')[0].strip()
sold_count = parser.xpath('//span[@class="reviewer--sold--ytPeoEy"]/text()')[0].strip()
return {
'title': title,
'price': price,
'rating': rating,
'total_reviews': total_reviews,
'sold_count': sold_count
}
# Sliocht Aitheantas Táirge ón URL
def extract_product_id(url):
return url.split('/')[-1].split('.')[0]
# Léirmheasanna Scrape
def scrape_reviews(product_id, page_num=1, page_size=10):
headers = {
'accept': 'application/json, text/plain, */*',
'referer': f'https://www.aliexpress.com/item/{product_id}.html',
'user-agent': 'Mozilla/5.0'
}
params = {
'productId': product_id,
'lang': 'en_US',
'page': str(page_num),
'pageSize': str(page_size),
}
response = requests.get('https://feedback.aliexpress.com/pc/searchEvaluation.do', params=params, headers=headers)
reviews = response.json()['data']['evaViewList']
return [review['buyerFeedback'] for review in reviews]
# Sábháil sonraí táirge agus athbhreithnithe chuig CSV
def save_to_csv(product_data, reviews, product_id):
pd.DataFrame([product_data]).to_csv(f'product_{product_id}_data.csv', index=False)
pd.DataFrame({'reviews': reviews}).to_csv(f'product_{product_id}_reviews.csv', index=False)
print(f'Saved into: product_{product_id}_data.csv')
print(f'Saved into: product_{product_id}_reviews.csv')
# Príomhfheidhm
async def main(url):
content = await get_page_content(url)
product_data = extract_product_data(content)
product_id = extract_product_id(url)
reviews = scrape_reviews(product_id)
save_to_csv(product_data, reviews, product_id)
# Rith an scraper
import asyncio
url = 'https://www.aliexpress.com/item/3256805354456256.html'
asyncio.run(main(url))
Agus sonraí á scríobadh, tá sé tábhachtach treoirlínte eiticiúla a leanúint:
Tar éis na dtreoirlínte seo cabhróidh sé leat scrape a dhéanamh go heiticiúil agus go freagrach, ag íoslaghdú na rioscaí d'úsáideoirí agus don chóras Aliexpress araon.
Tuairimí: 0