#!/usr/bin/env python
#coding:utf-8
import urllib2,re,sys,os,types
#from bs4 import BeautifulSoup                 

reload(sys);
sys.setdefaultencoding('gbk');              

province="上海"
city="上海"
fileHeader='\xEF\xBB\xBF'
colums='省直辖市^城市^行政区^商圈^名称^地址^联系人^联系电话^URL^公司介绍^'

def getCompany():
    for page in range(1,5+1):
        url1="http://book.jd.com/booktop-4-6929-%s.html"%(page)
        print "\n##################:",url1
        httpCrawler(url1,page) 

def httpCrawler(url,page):
    content = httpRequest(url)
    #<tr logr='j_2_27359935228167_20019655228034_3'>
    List=re.findall(r'<dt class=\'p-name\'>(.*?)<a href=\'(.*?)\' title="(.*?)" target=\'_blank\'',content,re.S)
    no=len(List)
    print no
    for i in range(0,no):#0 ~ no-1
    url=List[i][1]
    name=List[i][2]
    print "\ndownload one page:",List[i][1],"\n",List[i][2]
    if not os.path.exists('./jd'):
        os.mkdir(r'./jd')
    content = httpRequest(url)
#    if (page-1)*20+i+1 != 82:
        open(u'jd/%s.%s'%((page-1)*20+i+1,List[i][2].replace("/",""))+'.html','w+').write(content)
    print "ok"

def httpRequest(url):
    #try:
    html = None
    req_header = {
        'User-Agent':'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0',
        'Accept':'text/html;q=0.9,*/*;q=0.8',
        #'Accept-Language':'en-US,en;q=0.5',
        #'Accept-Encoding':'gzip',
        #'Host':'j3.s2.dpfile.com',
        #'Connection':'keep-alive'
        #'Referer':'http://www.baidu.com'
    }
    req_timeout = 15
    req = urllib2.Request(url,None,req_header)
    resp = urllib2.urlopen(req,None,req_timeout)
    html = resp.read()#.decode('gbk').encode('gbk')
    print "resp:",resp
    #print html
    #finally:
    #    if resp:
    #        resp.close()
    return html

def writeHeader(fileheader,colums):
    if not os.path.exists('./58'):
        os.mkdir(r'./58')
    f = open('./58/daikuan.csv', 'w')
    f.write(fileheader)
    f.write(colums)
    #f.write('\r\n')
    f.close()

if __name__ == '__main__':
    #writeHeader(fileHeader,colums)
    getCompany()

jd.py的更多相关文章

  1. day23单例模式 , 日志处理 , 项目结构目录

    # day23笔记 ## 一.补充,作业 ### 1.字符串格式化 ```pythonmsg = "我是%(n1)s,年龄%(n2)s" % {'n1': 'alex', 'n2' ...

  2. scrapy splash 之一二

    scrapy splash 用来爬取动态网页,其效果和scrapy selenium phantomjs一样,都是通过渲染js得到动态网页然后实现网页解析, selenium + phantomjs ...

  3. Python学习笔记(day23更新)

    第一章 计算机基础 1.1 硬件 计算机基本的硬件由:CPU / 内存 / 主板 / 硬盘 / 网卡 / 显卡 等组成,只有硬件但硬件之间无法进行交流和通信. 1.2 操作系统 作用:操作系统用于协同 ...

  4. python3之Splash

    Splash是一个javascript渲染服务.它是一个带有HTTP API的轻量级Web浏览器,使用Twisted和QT5在Python 3中实现.QT反应器用于使服务完全异步,允许通过QT主循环利 ...

  5. 用scrapy爬取京东的数据

    本文目的是使用scrapy爬取京东上所有的手机数据,并将数据保存到MongoDB中. 一.项目介绍 主要目标 1.使用scrapy爬取京东上所有的手机数据 2.将爬取的数据存储到MongoDB 环境 ...

  6. 正则爬取京东商品信息并打包成.exe可执行程序。

    本文爬取内容,输入要搜索的关键字可自动爬取京东网站上相关商品的店铺名称,商品名称,价格,爬取100页(共100页) 代码如下: import requests import re # 请求头 head ...

  7. Python使用request包请求网页乱码解决方法

    使用requests请求网页时,返回的页面信息有时是乱码,如下代码 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) App ...

  8. 用scrapy爬取京东商城的商品信息

    软件环境: gevent (1.2.2) greenlet (0.4.12) lxml (4.1.1) pymongo (3.6.0) pyOpenSSL (17.5.0) requests (2.1 ...

  9. 正则爬取京东商品信息并打包成.exe可执行程序

    本文爬取内容,输入要搜索的关键字可自动爬取京东网站上相关商品的店铺名称,商品名称,价格,爬取100页(共100页) 代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

随机推荐

  1. perl 安装 ZooKeeper模块

    1072 ./configure --libdir=/usr/lib 1073 make 1074 make install 1075 cpan ZooKeeper [root@wx03 c]# pe ...

  2. 安装基于XenServer的DevStack

    Openstack默认的hypervisior是基于KVM的,可以修改nova-compute.conf的libvirt_type改成使用其他,网上可以搜到个别文章 但是Openstack官方文档却说 ...

  3. c语言中scanf()、printf()函数

    函数调用scanf(“%d”,  &weight) 包含两个参数:“%d” 和&weight.C用逗号来隔开函数调用中的多个参数: 但是printf()和scanf()函数比较特殊,其 ...

  4. SQL Server 基础 03 查询数据基础

    查询数据 简单的查询 create table stu_info ( sno int not null ,sname ) not null ,sex ) not null ,birth ) not n ...

  5. 如何搭建Visual Studio的内核编程开发环境

    最近正在看<寒江独钓——Windows内核安全编程>这本书,感觉这本书非常好,有兴趣的朋友可以买来看看,有关这本书的信息请参考:http://www.china-pub.com/19559 ...

  6. QList 和std::list的比较

    QList QList<T> 是一个Qt通用容器类.它存储一序列的值,并且提供基于索引的数据访问方法和快速的插入和删除操作. QList<T>, QLinkedList< ...

  7. 解决yum升级的问题“There was a problem importing one of the Python modules”

    yum命令升级的时候,报出这个错误. There was a problem importing one of the Python modules required to run yum. The ...

  8. 区间dp模型之括号匹配打印路径 poj(1141)

    题目链接:Brackets Sequence 题目描写叙述:给出一串由'(')'' [ ' ' ] '组成的串,让你输出加入最少括号之后使得括号匹配的串. 分析:是区间dp的经典模型括号匹配.解说:h ...

  9. 推荐五个最好的MySQL GUI工具

    推荐五个最好的MySQL GUI工具 在本文中,我们会介绍一些最常用的MySQL GUI工具并附上下载链接.希望大家能更好的运用这些工具. AD:网+线下沙龙 | 移动APP模式创新:给你一个做APP ...

  10. sharepoint 2013 根据网站模版创建网站,并赋值网站权限 create a site by custom site template

    通过程序,根据网站模版,创建新的网站 private void CreateSiteBySiteTemplate() { SPSecurity.RunWithElevatedPrivileges(de ...