美文网首页爬虫专项
京东商品信息爬虫

京东商品信息爬虫

作者: 潇洒风 | 来源:发表于2017-08-14 11:58 被阅读31次

最近闲着在家无聊,就看看爬虫的书籍,突然发现很有趣,就写了许多代码,爬取了许多的网站,今天就分享爬取京东的源代码。

#京东商品信息爬虫  
#爬取京东商品信息并保存到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  

相关文章

  • 京东商品信息和评价采集爬虫源码

    /*使用javascript编写的爬虫源码,用于爬取京东商品信息和评价。 代码粘贴到神箭手云爬虫平台(http:/...

  • 京东商品信息爬虫

    最近闲着在家无聊,就看看爬虫的书籍,突然发现很有趣,就写了许多代码,爬取了许多的网站,今天就分享爬取京东的源代码。

  • 网络爬虫源码分享

    1.淘宝网商品爬虫源码 2.豌豆荚游戏排行榜爬虫源码 3.尚妆网爬虫源码 4.糗事百科爬虫源码 5.京东商品信息和...

  • (四)"淘宝商品信息定向爬虫"实例|Pyth

    1."淘宝商品信息定向爬虫"实例介绍2."淘宝商品信息定向爬虫"实例编写3."淘宝商品信息定向爬虫"实例数据解析4...

  • Scrapy+Selenium+Phantomjs的Demo

    前段时间学习了用Python写爬虫,使用Scrapy框架爬取京东的商品信息。商品详情页的价格是由js生成的,而通过...

  • 需要些例子

    教您使用java爬虫gecco抓取JD全部商品信息教您使用DynamicGecco抓取JD全部商品信息 Gecco...

  • 学习网址

    Python开源爬虫项目代码:抓取淘宝、京东、QQ、知网数据 scrapy_jingdong[9]- 京东爬虫。基...

  • 淘宝网商品爬虫源码

    使用javascript编写的爬虫源码,用于爬取淘宝网上的商品信息。 代码粘贴到神箭手云爬虫平台(http://w...

  • 沪商财富爬虫源码

    使用javascript编写的爬虫源码,用于爬取沪商财富网上的商品信息。 代码粘贴到神箭手云爬虫平台(http:/...

  • 尚妆网爬虫源码

    使用javascript编写的爬虫源码,用于爬取尚妆网上的商品信息。 代码粘贴到神箭手云爬虫平台(http://w...

网友评论

    本文标题: 京东商品信息爬虫

    本文链接:https://www.haomeiwen.com/subject/requrxtx.html