python爬虫学习之使用XPath解析开奖网站
实例需求:运用python语言爬取http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html这个开奖网站所有的信息,并且保存为txt文件。
实例环境:python3.7
BeautifulSoup库、XPath(需手动安装)
urllib库(内置的python库,无需手动安装)
实例网站:
第一步,点击链接http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html进入网站,查看网站基本信息,注意一共要爬取118页数据。

第二步,查看网页源代码,熟悉网页结构,标签等信息。

实例代码:
#encoding=utf-8
#pip install lxml
from bs4 import BeautifulSoup
import urllib.request
from lxml import etree class GetDoubleColorBallNumber(object):
def __init__(self):
self.urls = []
self.getUrls()
self.items = self.spider(self.urls)
self.pipelines(self.items) def getUrls(self):
URL = r'http://kaijiang.zhcw.com/zhcw/html/ssq/list.html'
htmlContent = self.getResponseContent(URL)
soup = BeautifulSoup(htmlContent, 'html.parser')
tag = soup.find_all('p')[-1]
pages = tag.strong.get_text()
pages = '3'
for i in range(2, int(pages)+1):
url = r'http://kaijiang.zhcw.com/zhcw/html/ssq/list_' + str(i) + '.html'
self.urls.append(url) #3、 网络模块(NETWORK)
def getResponseContent(self, url):
try:
response = urllib.request.urlopen(url)
except urllib.request.URLError as e:
raise e
else:
return response.read().decode("utf-8") #3、爬虫模块(Spider)
def spider(self,urls):
items = []
for url in urls:
try:
html = self.getResponseContent(url)
xpath_tree = etree.HTML(html)
trTags = xpath_tree.xpath('//tr[not(@*)]') # 匹配所有tr下没有任何属性的节点
for tag in trTags: # if tag.xpath('../html'):
# print("找到了html标签")
# if tag.xpath('/td/em'):
# print("****************") #如果存在em子孙节点
if tag.xpath('./td/em'):
item = {}
item['date'] = tag.xpath('./td[1]/text()')[0]
item['order'] = tag.xpath('./td[2]/text()')[0]
item['red1'] = tag.xpath('./td[3]/em[1]/text()')[0]
item['red2'] = tag.xpath('./td[3]/em[2]/text()')[0]
item['red3'] = tag.xpath('./td[3]/em[3]/text()')[0]
item['red4'] = tag.xpath('./td[3]/em[4]/text()')[0]
item['red5'] = tag.xpath('./td[3]/em[5]/text()')[0]
item['red6'] = tag.xpath('./td[3]/em[6]/text()')[0]
item['blue'] = tag.xpath('./td[3]/em[7]/text()')[0]
item['money'] = tag.xpath('./td[4]/strong/text()')[0]
item['first'] = tag.xpath('./td[5]/strong/text()')[0]
item['second'] = tag.xpath('./td[6]/strong/text()')[0]
items.append(item)
except Exception as e:
print(str(e))
raise e
return items def pipelines(self,items):
fileName = u'双色球.txt'
with open(fileName, 'w') as fp:
for item in items:
fp.write('%s %s \t %s %s %s %s %s %s %s \t %s \t %s %s \n'
%(item['date'],item['order'],item['red1'],item['red2'],item['red3'],item['red4'],item['red5'],item['red6'],item['blue'],item['money'],item['first'],item['second'])) if __name__ == '__main__':
GDCBN = GetDoubleColorBallNumber()
实例结果:


python爬虫学习之使用XPath解析开奖网站的更多相关文章
- Python爬虫学习之使用beautifulsoup爬取招聘网站信息
菜鸟一只,也是在尝试并学习和摸索爬虫相关知识. 1.首先分析要爬取页面结构.可以看到一列搜索的结果,现在需要得到每一个链接,然后才能爬取对应页面. 关键代码思路如下: html = getHtml(& ...
- python爬虫学习之使用BeautifulSoup库爬取开奖网站信息-模块化
实例需求:运用python语言爬取http://kaijiang.zhcw.com/zhcw/html/ssq/list_1.html这个开奖网站所有的信息,并且保存为txt文件和excel文件. 实 ...
- python爬虫学习笔记(一)——环境配置(windows系统)
在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库) [推荐地址:清华镜像] https://mirrors ...
- Python爬虫学习:三、爬虫的基本操作流程
本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...
- Python爬虫教程-22-lxml-etree和xpath配合使用
Python爬虫教程-22-lxml-etree和xpath配合使用 lxml:python 的HTML/XML的解析器 官网文档:https://lxml.de/ 使用前,需要安装安 lxml 包 ...
- 小白学 Python 爬虫(21):解析库 Beautiful Soup(上)
小白学 Python 爬虫(21):解析库 Beautiful Soup(上) 人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前 ...
- 小白学 Python 爬虫(22):解析库 Beautiful Soup(下)
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(23):解析库 pyquery 入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- python爬虫学习01--电子书爬取
python爬虫学习01--电子书爬取 1.获取网页信息 import requests #导入requests库 ''' 获取网页信息 ''' if __name__ == '__main__': ...
随机推荐
- json转换对象中出现null属性的解决方法
前言:当数据进行json转换时,当属性值为null时,json解析就会中断,导致接下来的数据无法正确获取.原则上来讲服务器端发送的json字符串不允许存在属性值为空的情况,但是如果服务器端发送了nul ...
- 复制粘贴插件(不包含 Flash)——clipboard.js
clipboard.js是现代化的“复制到剪切板”插件.不包含 Flash.gzip 压缩后仅 3kb.不依赖 Flash 或其他臃肿的框架.API:https://clipboardjs.com c ...
- angular ViewChild ContentChild 系列的查询参数
官方说明 官方文档 在调用 NgAfterViewInit 回调函数之前就会设置这些视图查询. 元数据属性: selector - 用于查询的指令类型或名字. read - 从查询到的元素中读取另一个 ...
- MySQL索引优化步骤总结
在项目使用mysql过程中,随着系统的运行,发现一些慢查询,在这里总结一下mysql索引优化步骤 1.开发过程优化 开发过程中对业务表中查询sql分析sql执行计划(尤其是业务流水表),主要是查看sq ...
- node.js 调试 eggs launch.json配置信息
{ // 使用 IntelliSense 了解相关属性. // 悬停以查看现有属性的描述. // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linki ...
- paxos made more simple
paxos算法是进入分布式领域的一块基石,有关paxos的讨论有很多精彩的详细论述,很多牛人不惜宝贵时间以大幅详尽段落叙述.感谢他们,paxos more simple 理解paxos前,我建议以面到 ...
- python数据结构(二)------列表
本文将重点梳理列表及列表操作. 2.1 list函数 2.2 基本列表操作 2.3 列表方法 2.1 list函数 >>>list('hello') ['h','e','l',l', ...
- Jquery 数组操作大全【转载】
转载于:https://www.jb51.net/article/43164.htm 1. $.each(array, [callback]) 遍历[常用] 解释: 不同于例遍 jQuery 对象的 ...
- idea在debugger模式下无法启动,但是在run模式下可以启动的问题
debugger模式下,启动idea,总是报内存溢出异常, Error creating bean with name 'sysRoleUserMapper' defined in URL [jar: ...
- thymelead入门 git地址在文档最后
流程:##### 流程###### 1:pom添加依赖 <dependency> <groupId>org.springframework.boot</groupId&g ...