python爬取京东网页并用pyecharts分析数据
时间: 2023-07-29 15:10:52 浏览: 99
Python爬取京东
好的,以下是我提供的一些爬取京东网页并用pyecharts分析数据的步骤:
1. 导入所需的库
```python
import requests
from pyquery import PyQuery as pq
from pyecharts import options as opts
from pyecharts.charts import Bar
```
2. 定义爬取函数并解析数据
```python
def get_jd_info(keyword):
url = 'https://search.jd.com/Search?keyword={}&enc=utf-8'.format(keyword)
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'}
r = requests.get(url, headers=headers)
html = r.text
doc = pq(html)
items = doc('.gl-item').items()
data = []
for item in items:
title = item.find('.p-name em').text().replace('\n','')
price = item.find('.p-price i').text()
data.append({'title':title, 'price':price})
return data
```
3. 调用函数并将数据可视化
```python
keyword = '手机'
data = get_jd_info(keyword)
prices = [float(item['price']) for item in data]
titles = [item['title'] for item in data]
# 使用pyecharts绘制柱状图
bar = (
Bar()
.add_xaxis(titles)
.add_yaxis("价格", prices)
.set_global_opts(title_opts=opts.TitleOpts(title="{}价格分布".format(keyword)))
)
bar.render("{}.html".format(keyword))
```
以上是一个简单的例子,你可以根据自己的需求对代码进行修改和优化。
阅读全文