下面是一个关于小红书商家爬虫的程序的示例:
import requests
from bs4 import BeautifulSoupdef get_seller_data():url = 'https://www.xiaohongshu.com/sellers'headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}response = requests.get(url, headers=headers)soup = BeautifulSoup(response.text, 'html.parser')sellers = soup.find_all('div', class_='seller-card')for seller in sellers:name = seller.find('div', class_='seller-card__name').text.strip()products = seller.find_all('a', class_='seller-card__product')product_names = [product.text.strip() for product in products]print(f"Seller: {name}")print(f"Products: {', '.join(product_names)}")print("--------------------")get_seller_data()
这个程序会爬取小红书的商家页面(https://www.xiaohongshu.com/sellers),并提取出每个商家的名称和其所售商品的名称。在终端输出结果时,商家名称和商品名称之间会用逗号分隔,并用“--------------------”分隔不同商家的信息。
注意,使用爬虫程序时,应该要遵循网站的使用条款,并确保不会对网站造成任何不利影响。在实际使用中,可以适当添加一些延时等操作来降低对网站的负载。