Python爬虫之豆瓣-新书速递-图书解析
1- 问题描述
抓取豆瓣“新书速递”[1]页面下图书信息(包括书名,作者,简介,url),将结果重定向到txt文本文件下。
2- 思路分析[2]
Step1 读取HTML
Step2 Xpath遍历元素和属性
3- 使用工具
Python,lxml模块,requests模块
4- 程序实现
# -*- coding: utf-8 -*-
from lxml import html
import requests page = requests.get('http://book.douban.com/latest?icn=index-latestbook-all')
tree = html.fromstring(page.text) # 若保存了html文件,可使用下面方法
# page = open('/home/freyr/codeHouse/python/512.htm', 'r').read()
# tree = html.fromstring(page) #提取图书信息
bookname = tree.xpath('//div[@class="detail-frame"]/h2/text()') # 书名
author = tree.xpath('//div[@class="detail-frame"]/p[@class="color-gray"]/text()') # 作者
info = tree.xpath('//div[@class="detail-frame"]/p[2]/text()') # 简介
url = tree.xpath('//ul[@class="cover-col-4 clearfix"]/li/a[@href]') # URL booknames = map(lambda x:x.strip(), bookname)
authors = map(lambda x:x.strip(), author)
infos = map(lambda x:x.strip(), info)
urls = map(lambda p: p.values()[0], url) with open('/home/freyr/codeHouse/python/dbBook.txt','w+') as f:
for book, author, info, url in zip(booknames, authors, infos, urls):
f.write('%s\n\n%s\n\n%s' % (book.encode('utf-8'), author.encode('utf-8'), info.encode('utf-8')))
f.write('\n\n%s\n' % url )
f.write('\n\n-----------------------------------------\n\n\n')
PS: 1.还没有真正入手学习网页爬虫,先简单记录下。
2.程序涉及编码问题[3]
[1] 豆瓣-新书速递
[3] lxml 中文乱码
Python爬虫之豆瓣-新书速递-图书解析的更多相关文章
- Python爬虫(四)——豆瓣数据模型训练与检测
前文参考: Python爬虫(一)——豆瓣下图书信息 Python爬虫(二)——豆瓣图书决策树构建 Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析 数据的构建 在这张表中我们可以发现 ...
- 用Python爬虫对豆瓣《敦刻尔克》影评进行词云展示
最近很想看的一个电影,去知乎上看一下评论,刚好在学Python爬虫,就做个小实例. 代码基于第三方修改 原文链接 http://python.jobbole.com/88325/#comment-9 ...
- Python爬虫(一)——豆瓣下图书信息
爬虫目的: 随着近年互联网的发展,网络上的信息飞速数量增长.在庞大的数据面前想要获得期望的信息往往如同大海捞针.通过合理的筛选,在百万甚至数亿计的数据中找到所需信息,无疑有着非常大的意义. 在豆瓣网下 ...
- Python爬虫(二)——豆瓣图书决策树构建
前文参考: https://www.cnblogs.com/LexMoon/p/douban1.html Matplotlib绘制决策树代码: # coding=utf-8 import matpl ...
- python爬虫之一---------豆瓣妹子图
#-*- coding:utf-8 -*- __author__ = "carry" import urllib import urllib2 from bs4 import Be ...
- Python爬虫——爬豆瓣登录页面
直接上代码 import urllib.request import http.cookiejar from lxml import etree # from spiderImg import get ...
- Python 爬虫:豆瓣电影Top250,包括电影导演、类型、年份、主演
结果输出到文本文件中. import codecs import requests from bs4 import BeautifulSoup headers={'User-Agent': 'Mozi ...
- Python爬虫_豆瓣电视剧
1 import requests 2 import json 3 import csv 4 5 6 class DoubantvSpider: 7 def __init__(self): 8 # s ...
- Python爬虫系列(七):提高解析效率
如果仅仅因为想要查找文档中的<a>标签而将整片文档进行解析,实在是浪费内存和时间.最快的方法是从一开始就把<a>标签以外的东西都忽略掉. SoupStrainer 类可以定义文 ...
随机推荐
- Android之BroadcastReceiver1
1.触发发送广播 public class MainActivity extends Activity { private Button sendButton; @Override protected ...
- Python标准库08 多线程与同步 (threading包)
Python主要通过标准库中的threading包来实现多线程.在当今网络时代,每个服务器都会接收到大量的请求.服务器可以利用多线程的方式来处理这些请求,以提高对网络端口的读写效率.Python是一种 ...
- c#图片上绘制半透明矩形
p.CreateGraphics().FillRectangle( ,Color.LightGreen)), iLeft, iTop, iRight - iLeft, iBottom - iTop); ...
- zkclient
zkclient 快速指南 Maven依赖 最新的版本发布在Maven中央库. <dependency> <groupId>com.github.adyliu</grou ...
- Celery 使用简介
转自:http://liuzxc.github.io/blog/celery/ Celery 是一个简单.灵活且可靠的,处理大量消息的分布式系统,它是一个专注于实时处理的任务队列, 同时也支持任务调度 ...
- POJ 1611
菜鸟第一次做这种.想了好一会儿.== 首先还是初始化记忆数组,使得每一个元素的初始根节点是自己. 然后是对输入的数据进行并集.我们拿出每组元素的第一个作为根节点. 每次检测是否已经存在根节点.如果存在 ...
- poj 1094 Sorting It All Out(nyoj 349)
点击打开链接 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24544 Accep ...
- java String 的方法 (01)
1.int compareTo(String other) 按照字典的顺序,如果字符串位于String other 之前,则返回一个负数,如果字符串位于othere之后,则返回一个正数,如果两个字符串 ...
- win10 TortoiseSVN 部分图标不显示
原因:https://msdn.microsoft.com/en-us/library/cc144123(VS.85).aspx Note The number of different icon ...
- cocos2d-lua 3.5 android搭建常见错误
新建一个项目,就不说了,就是用命令行 cocos new HelloLua -p com.wwj.hellolua -l lua -d ~/Cocos2dxProj ,生成下 然后把项目导入eclip ...