Python京东手机数据抓取
时间: 2023-12-25 11:28:20 浏览: 104
以下是使用Python爬取京东手机数据的步骤:
1. 导入必要的库
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
```
2. 获取页面信息
```python
url = 'https://search.jd.com/Search?keyword=手机&enc=utf-8&wq=手机&pvid=7e9b5f6f5d3c4d7e9c5d3c4d7e9b5f6f'
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'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
```
3. 解析页面信息
```python
goods_list = soup.find_all('li', class_='gl-item')
data = []
for item in goods_list:
try:
name = item.find('div', class_='p-name').find('em').text.strip()
price = item.find('div', class_='p-price').find('i').text
shop = item.find('div', class_='p-shop').find('a').text.strip()
commit = item.find('div', class_='p-commit').find('a').text.strip()
data.append([name, price, shop, commit])
except:
pass
```
4. 将数据存储到Excel表格中
```python
df = pd.DataFrame(data, columns=['商品名称', '价格', '店铺', '评论数'])
df.to_excel('jd_phone.xlsx', index=False)
```
阅读全文