京东商品信息爬虫

本文实现一个京东商品信息爬虫,通过python实现,可以交互式的输入要查询的商品,查询信息保存到csv文件中。
#京东商品信息爬虫
#爬取京东商品信息并保存到csv格式文件中
#2017-7-23


import os
import requests
import csv
from bs4 import BeautifulSoup

#获取url请求
def gethtml(kind,page):
    '''获取url请求'''
    pagenum = str(2 * page)
    try:
        r = requests.get('https://search.jd.com/Search?keyword=' + \
        kind + '&enc=utf-8&page=' + pagenum)#链接url
        r.raise_for_status()
        r.encoding = r.apparent_encoding#转码
        print('爬取第{}页:'.format(page))
        return r.text#返回html
    except:
        print('链接异常!!!')
        return ''

#获取定位资源
def findhtml(html,httplist):
    """寻找资源"""
    soup = BeautifulSoup(html,'lxml')
    links = soup.find_all('div', class_='gl-i-wrap')#寻找'div'标签
    for link in links:
        ui = []
        namediv = link.find('div', class_='p-name p-name-type-2')#寻找商品名称和链接
        title = namediv.a['title']
        href = namediv.a['href']
        ui.append(title)#名称加入到ui中
        pricediv = link.find('div', class_='p-price')#寻找商品价格
        try:
            price =  pricediv.strong['data-price'] 
            ui.append(price)#价格加入到ui中
        except:
            ui.append('')
        if 'https:' not in href:#添加链接
            ui.append('https:' + href)
        else:
            ui.append(href)
        aggressmentdiv = link.find('div', class_='p-commit')#寻找评论
        number = aggressmentdiv.strong.contents[1].string
        ui.append(number)#评论数添加到ui中
        httplist.append(ui)
        try:
            if price:
                print('{:^10s}:{:<}元'.format(title,price))
            else:
                print('{:^10s}'.format(title))
        except:
            print('{:^10s}'.format(title))


#保存资源
def savehtml(ul):
    path = 'D:/数据/'
    if not os.path.exists(path):
        os.mkdir(path)#创建一个文件
    with open(path + '京东商品信息爬虫.csv','w+') as f:
        writer = csv.writer(f)
        writer.writerow(['商品','价格','链接','评价数'])
        for u in range(len(ul)):
            if ul[u]:
                writer.writerow([ul[u][0],ul[u][1],ul[u][2],ul[u][3]])



#程序主体
if __name__ == '__main__':
    goods = input('请输入要搜索的物品:')
    yeshu = int(input('请输入要查询到的页数:'))
    ulist = []
    for i in range(yeshu+1):
        try:
            if i != 0:
                text = gethtml(goods,i)
                findhtml(text,ulist)
            savehtml(ulist)
        except:
            break

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值