首页 > 大数据 > 数据挖掘 > 拼多多采集使用指南 拼多多商家采集设计思路
import requestsfrom bs4 import BeautifulSoupimport csv# 发送请求,并获取返回的HTMLurl = "https://pinduoduo.com/search?keyword=手机"response = requests.get(url)html = response.text# 解析HTML,提取商品信息soup = BeautifulSoup(html, "html.parser")products = soup.find_all("div", class_="product-item")# 存储商品信息filename = "pinduoduo_products.csv"with open(filename, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(("商品名称", "商品价格", "商品链接"))
for product in products:
name = product.find("a").text
price = product.find("span", class_="price").text
link = url + product.find("a")["href"]
writer.writerow((name, price, link))
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Selectors;public class AlibabaCrawler {
public static void main(String[] args) {
String keyword = "连衣裙";
String url = "https://s.1688.com/selloffer/serp.htm?keywords=" + keyword;
try {
// 建立连接并获取页面内容
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine())!= null) {
response.append(inputLine);
}
in.close();
// 使用Jsoup解析页面
Document doc = Jsoup.parse(response.toString());
// 查找商品列表元素并提取信息
for (Element product : doc.select(Selectors.byClass("offer-list-item"))) {
String product_name = product.select(Selectors.byClass("offer-title-link")).text().trim();
String product_price = product.select(Selectors.byClass("price")).text().trim();
String product_company = product.select(Selectors.byClass("company-name")).text().trim();
System.out.println("商品名称:" + product_name);
System.out.println("商品价格:" + product_price);
System.out.println("商家名称:" + product_company);
}
} catch (IOException e) {
e.printStackTrace();
}
}}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70042581/viewspace-3051901/ ,如需转载,请注明出处,否则将追究法律责任。