资源简介

Python豆瓣爬虫以及爬虫爬好的图书分类数据,文件格式为xlsl,含分类、图书名、作者名、出版社、评分等信息

资源截图

代码片段和文件信息

#-*- coding: UTF-8 -*-

import sys
import time
import urllib
import urllib2
import requests
import numpy as np
from bs4 import BeautifulSoup
from openpyxl import Workbook

reload(sys)
sys.setdefaultencoding(‘utf8‘)



#Some User Agents
hds=[{‘User-Agent‘:‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘}\
{‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.11 (KHTML like Gecko) Chrome/17.0.963.12 Safari/535.11‘}\
{‘User-Agent‘: ‘Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)‘}]


def book_spider(book_tag):
    page_num=0;
    book_list=[]
    try_times=0
    
    while(1):
        #url=‘http://www.douban.com/tag/%E5%B0%8F%E8%AF%B4/book?start=0‘ # For Test
        url=‘http://www.douban.com/tag/‘+urllib.quote(book_tag)+‘/book?start=‘+str(page_num*15)
        time.sleep(np.random.rand()*5)
        
        #Last Version
        try:
            req = urllib2.Request(url headers=hds[page_num%len(hds)])
            source_code = urllib2.urlopen(req).read()
            plain_text=str(source_code)   
        except (urllib2.HTTPError urllib2.URLError) e:
            print e
            continue
  
        ##Previous Version IP is easy to be Forbidden
        #source_code = requests.get(url) 
        #plain_text = source_code.text  
        
        soup = BeautifulSoup(plain_text)
        list_soup = soup.find(‘div‘ {‘class‘: ‘mod book-list‘})
        
        try_times+=1;
        if list_soup==None and try_times<200:
            continue
        elif list_soup==None or len(list_soup)<=1:
            break # Break when no informatoin got after 200 times requesting
        
        for book_info in list_soup.findAll(‘dd‘):
            title = book_info.find(‘a‘ {‘class‘:‘title‘}).string.strip()
            desc = book_info.find(‘div‘ {‘class‘:‘desc‘}).string.strip()
            desc_list = desc.split(‘/‘)
            book_url = book_info.find(‘a‘ {‘class‘:‘title‘}).get(‘href‘)
            
            try:
                author_info = ‘作者/译者: ‘ + ‘/‘.join(desc_list[0:-3])
            except:
                author_info =‘作者/译者: 暂无‘
            try:
                pub_info = ‘出版信息: ‘ + ‘/‘.join(desc_list[-3:])
            except:
                pub_info = ‘出版信息: 暂无‘
            try:
                rating = book_info.find(‘span‘ {‘class‘:‘rating_nums‘}).string.strip()
            except:
                rating=‘0.0‘
            try:
                #people_num = book_info.findAll(‘span‘)[2].string.strip()
                people_num = get_people_num(book_url)
                people_num = people_num.strip(‘人评价‘)
            except:
                people_num =‘0‘
            
            book_list.append([titleratingpeople_numauthor_infopub_info])
            try_times=0 #set 0 when got valid information
        page_num+=1
        print ‘Downloading Information From Page %d‘ % page_num
    return book_list


def get_people_num(url):
    #url=‘http://book.douban.com/subj

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-04-24 17:40  douban-spider-and-statics\
     文件      205376  2018-12-07 13:29  douban-spider-and-statics\book_list-个人管理-时间管理-投资-文化-宗教.xlsx
     文件     4149089  2018-12-07 13:29  douban-spider-and-statics\book_list-传记-哲学-编程-创业-理财-社会学-佛教.xlsx
     文件       40548  2018-12-07 13:29  douban-spider-and-statics\book_list-名著.xlsx
     文件      107861  2018-12-07 13:29  douban-spider-and-statics\book_list-商业-理财-管理.xlsx
     文件     5495727  2018-12-07 13:29  douban-spider-and-statics\book_list-心理-判断与决策-算法-数据结构-经济-历史.xlsx
     文件     1833209  2018-12-07 13:29  douban-spider-and-statics\book_list-思想-科技-科学-web-股票-爱情-两性.xlsx
     文件      379429  2018-12-07 13:29  douban-spider-and-statics\book_list-摄影-设计-音乐-旅行-教育-成长-情感-育儿-健康-养生.xlsx
     文件       55049  2018-12-07 13:29  douban-spider-and-statics\book_list-数学.xlsx
     文件      125082  2018-12-07 13:29  douban-spider-and-statics\book_list-科幻-思维-金融.xlsx
     文件      188029  2018-12-07 13:29  douban-spider-and-statics\book_list-科普-经典-生活-心灵-文学.xlsx
     文件      890982  2018-12-07 13:29  douban-spider-and-statics\book_list-计算机-机器学习-linux-android-数据库-互联网.xlsx
     文件        5331  2018-12-07 13:29  douban-spider-and-statics\doubanSpider.py

评论

共有 条评论