基于python京东香水数据的采集与分析 数据爬取实现
时间: 2023-08-05 07:09:20 浏览: 62
在基于Python的京东香水数据采集方面,你可以使用Python的网络爬虫库来实现。以下是一个简单的示例代码,使用Requests和BeautifulSoup库来爬取京东香水的商品数据:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头,模拟浏览器访问
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'
}
def get_product_data(url):
# 发送GET请求,获取页面内容
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取商品数据
products = soup.find_all('div', class_='gl-i-wrap')
for product in products:
# 提取商品名称
name = product.find('div', class_='p-name').text.strip()
# 提取商品价格
price = product.find('div', class_='p-price').strong.i.text.strip()
# 提取商品评价数量
comment_count = product.find('div', class_='p-commit').a.text.strip()
# 打印商品数据
print(f"名称:{name}")
print(f"价格:{price}")
print(f"评价数量:{comment_count}")
print("-----------------------")
# 爬取第一页的商品数据
url = "https://search.jd.com/Search?keyword=%E9%A6%99%E6%B0%B4&enc=utf-8&wq=%E9%A6%99%E6%B0%B4&pvid=5d4a0f1c7c8b4b8e9b3f086a8d90f6d5"
get_product_data(url)
```
上述示例代码中,首先设置了请求头,模拟浏览器的访问,然后使用Requests库发送GET请求,获取京东香水商品页面的内容。接着使用BeautifulSoup库解析页面内容,并通过查找对应的HTML元素提取出商品名称、价格和评价数量等数据。最后打印出商品数据。
你可以根据自己的需求对代码进行修改和扩展,比如添加翻页功能、保存数据到文件或数据库等。希望对你有帮助!如有任何疑问,请随时向我提问。