爬取京东商品信息
环境:
- Python 3.6
- Pycharm
- MYSQL
京东网页分析
主要抓取以下商品参数:
name : 商品种类名
price : 商品价格
location: 商品图片地址
shop_name: 商品描述
爬取京东商品信息首先得有商品信息入口,以商品女装(关键字)为例,
url = 'https://search.jd.com/Search?keyword=%s&enc=utf-8&page=%s'
# keyword 为搜索关键字
# page 为页码
简单分析京东商品信息页面,发现商品讯息:
我们可以通过xpath找到我们想要的信息:
找到我们想要的信息后我们通过PyMysql将商品信息存到数据库中:
def get_db(self):
db = pymysql.connect(
host='你的ip',
port=3306,
user='用户名',
password='密码',
db='库名',
charset='utf8',
)
return db
def result_save(self, data):
#连接数据库
db = self.get_db()
#创建游标
cursor = db.cursor()
#sql语句
sql = 'insert into taobao (name, price, shop_name, location) values(%s, %s, %s, %s)'
try:
for i in range(len(data['price'])):