Python 2.7_利用xpath语法爬取豆瓣图书top250信息_20170129
大年初二,忙完家里一些事,顺带有人交流爬取豆瓣图书top250
1、构造urls列表 urls=['https://book.douban.com/top250?start={}'.format(str(i) for i in range(0, 226, 25))]
2、模块 requests获取网页源代码 lxml 解析网页 xpath提取
3、提取信息
4、可以封装成函数 此处没有封装调用
python代码:
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from lxml import etree
import requests
urls=['https://book.douban.com/top250?start={}'.format(str(i) for i in range(0, 226, 25))]
for url in urls:
html=requests.get(url).content
selector=etree.HTML(html)
infos=selector.xpath('//tr[@class="item"]')
for info in infos:
book_name = info.xpath('td/div/a/@title')[0]
book_url = info.xpath('td/div/a/@href')[0]
published_infos = str(info.xpath('td/p/text()')[0])
splitlistinfos = published_infos.split('/')
#print splitlistinfos
published_date=str(splitlistinfos[-2])
#print published_date
price = str(splitlistinfos[-1])
#print price
rate = info.xpath('td/div/span[2]/text()')[0]
# comment_nums = info.xpath('td/div/span[3]/text()')[0]
# print comment_nums
comment_nums = info.xpath('td/div/span[3]/text()')[0].strip('(').strip().strip(')').strip().strip('人评价').strip() +'人评价'
introduceinfo = info.xpath('td/p/span/text()')
print book_name,book_url,published_date,price,rate,comment_nums,introduceinfo[0] if len(introduceinfo) > 0 else ''

Python 2.7_利用xpath语法爬取豆瓣图书top250信息_20170129的更多相关文章
- python系列之(3)爬取豆瓣图书数据
上次介绍了beautifulsoup的使用,那就来进行运用下吧.本篇将主要介绍通过爬取豆瓣图书的信息,存储到sqlite数据库进行分析. 1.sqlite SQLite是一个进程内的库,实现了自给自足 ...
- Scrapy中用xpath/css爬取豆瓣电影Top250:解决403HTTP status code is not handled or not allowed
好吧,我又开始折腾豆瓣电影top250了,只是想试试各种方法,看看哪一种的方法效率是最好的,一直进行到这一步才知道 scrapy的强大,尤其是和selector结合之后,速度飞起.... 下面我就采用 ...
- Python爬虫-爬取豆瓣图书Top250
豆瓣网站很人性化,对于新手爬虫比较友好,没有如果调低爬取频率,不用担心会被封 IP.但也不要太频繁爬取. 涉及知识点:requests.html.xpath.csv 一.准备工作 需要安装reques ...
- 爬取豆瓣电影Top250信息
# -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import requests from pyquery import PyQ ...
- python爬虫1——获取网站源代码(豆瓣图书top250信息)
# -*- coding: utf-8 -*- import requests import re import sys reload(sys) sys.setdefaultencoding('utf ...
- Python爬虫小白入门(七)爬取豆瓣音乐top250
抓取目标: 豆瓣音乐top250的歌名.作者(专辑).评分和歌曲链接 使用工具: requests + lxml + xpath. 我认为这种工具组合是最适合初学者的,requests比pytho ...
- python 爬虫&爬取豆瓣电影top250
爬取豆瓣电影top250from urllib.request import * #导入所有的request,urllib相当于一个文件夹,用到它里面的方法requestfrom lxml impor ...
- Python爬虫入门:爬取豆瓣电影TOP250
一个很简单的爬虫. 从这里学习的,解释的挺好的:https://xlzd.me/2015/12/16/python-crawler-03 分享写这个代码用到了的学习的链接: BeautifulSoup ...
- scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250
scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250 前言 经过上一篇教程我们已经大致了解了Scrapy的基本情况,并写了一个简单的小demo.这次我会以爬取豆瓣电影TOP250为例进一步为大 ...
随机推荐
- LeetCode:为运算表达式设置优先级【241】
LeetCode:为运算表达式设置优先级[241] 题目描述 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 ...
- 17 南宁区域赛 F - The Chosen One 【规律】
题目链接 https://nanti.jisuanke.com/t/19972 题意 给出一个n 然后将 n 个数 标号为 1 -> n 按顺序排列 每次抽掉 奇数位的数 然后求最后剩下那个数字 ...
- iOS 关于自动更新的分阶段发布(灰度发布)的相关简介
前言: AppStore 发布应用方式除了自动和手动,如今添加了分阶段发布(灰度发布).目的很明确,降低新版本骤然上升的bug率,不能挽回,只能发布新版本的风险.也也是针对禁止使用热修复,推出的相对 ...
- 混淆Android JAR包的方法
http://blog.csdn.net/vurtne_ye/article/details/35599491 1)导出jar包 如何在eclipse上Android工程中导出jar包?google, ...
- PHP中的常见魔术方法功能作用及用法实例
概述 在面向对象编程中,PHP提供了一系列的魔术方法,这些魔术方法为编程提供了很多便利.PHP中的魔术方法通常以__(两个下划线)开始,并且不需要显示的调用而是由某种特定的条件出发. 开始之前 在总结 ...
- myisam表修复
数据库myisam引擎表损坏修复步骤: 1.进入到表目录文件下 # myisamchk -of comments.MYI 2. # myisamchk -r comments.MYI 3. # ...
- 20145239杜文超《网络对抗》- shellcode注入&Return-to-libc攻击深入
20145239杜文超<网络对抗>- shellcode注入&Return-to-libc攻击深入 shellcode基础知识 Shellcode是一段代码,作为数据发送给受攻击服 ...
- different between web api and web service
https://stackoverflow.com/questions/19336347/what-is-the-difference-between-a-web-api-and-a-web-ser ...
- shell删除最后一列、删除第一行、比较文件
删除文件第一行: sed -i '1d' filename 删除文件最后一列: awk '{print $NF}' filename 比较文件的方法: 1)comm -3 --nocheck-orde ...
- processing学习整理---Image
1.Load and Display(加载与显示) Images can be loaded and displayed to the screen at their actual size or ...