Python语言示例:
import requests
from bs4 import BeautifulSoup
url = "https://www.example.com/product/12345"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 抓取商品名称
product_name = soup.find("h1", {"class": "product-title"}).text
print("商品名称:", product_name)
# 抓取商品价格
product_price = soup.find("span", {"class": "price-包括数字的价格}).text
print("商品价格:", product_price)
# 抓取商品库存
product_stock = soup.find("span", {"class": "in-stock"}).text
print("商品库存:", product_stock)
JavaScript语言示例:
const url = "https://www.example.com/product/12345";
fetch(url)
.then(response => response.text())
.then(html => {
const productName = html.querySelector(".product-title").textContent;
const productPrice = html.querySelector(".price-包括数字的价格").textContent;
const productStock = html.querySelector(".in-stock").textContent;
console.log("商品名称:", productName);
console.log("商品价格:", productPrice);
console.log("商品库存:", productStock);
});
Java语言示例:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ProductInfo {
public static void main(String[] args) {
String url = "https://www.example.com/product/12345";
Document doc = Jsoup.connect(url).get();
Element productName = doc.select(".product-title").first();
Element productPrice = doc.select(".price-包括数字的价格").first();
Element productStock = doc.select(".in-stock").first();
System.out.println("商品名称: " + productName.text());
System.out.println("商品价格: " + productPrice.text());
System.out.println("商品库存: " + productStock.text());
}
}
以上是使用 Python、JavaScript 和 Java 三种语言抓取商品详情的示例,其中用到了对应语言的requests、BeautifulSoup、fetch和Jsoup等库或框架来获取网页内容,并使用相应的方法或属性来获取需要的数据。
R语言示例:
library(httr)
library(XML)
url <- "https://www.example.com/product/12345"
response <- GET(url)
html <- content(response, as = "text", type = "html")
doc <- htmlParse(html)
product_name <- getNodeSet(doc, "//div[@class='product-title']")[[1]]$children[2]@content
product_price <- getNodeSet(doc, "//div[@class='price-包括数字的价格']")[[1]]$children[2]@content
product_stock <- getNodeSet(doc, "//div[@class='in-stock']")[[1]]$children[2]@content
cat("商品名称:", product_name, "\n")
cat("商品价格:", product_price, "\n")
cat("商品库存:", product_stock, "\n")