Python 通过lxml遍历html xpath
#coding:utf-8
'''
Created on 2017年10月9日 @author: li.liu
'''
from selenium import webdriver
from lxml import etree
import urllib
import urllib2
import time #url='http://www.woyihome.com'
url='http://sso.woyihome.com/sso/pc-login'
#url='http://www.baidu.com'
user_agent='Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
values = {'name' : 'WHY',
'location' : 'SDU',
'language' : 'Python' } headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
html1= response.read().encode('utf-8') def test1():
x1={} #html1=urllib.urlopen(url).read().decode('utf-8')
#print html1
hxml=etree.HTML(html1)
#print hxml
htree=etree.ElementTree(hxml)
#print htree
id_dite=htree.xpath('//*[@id]')
#print id_dite
coun=0
for id_items in id_dite:
#print id_items.items()
#print htree.getpath(id_items)
for id_item in id_items.items():
#print id_item
if id_item[0]=='id':
id_str='//*[@id="'+id_item[1]+'"]'
x1[id_str]=[]
#print id_str
id_path=htree.getpath(htree.xpath(id_str)[0])
#print id_path
id_str1=id_str+'//*'
idelem_list=htree.xpath(id_str1)
#print idelem_list
for e in idelem_list:
if len(e.items())==0:
pass
else:
e_path=htree.getpath(e)
#print e_path
e_path1=e_path.split(id_path)
#print e_path1[1]
if len(e_path1)>1:
e_str=id_str+e_path1[1]
e_list=e_str.split('/')
if 'li' in e_list[len(e_list)-1] or 'ul' in e_list[len(e_list)-1] or 'span' in e_list[len(e_list)-1]:
pass
else:
#print e_str
coun+=1
x1[id_str].append(e_str)
'''
for i in x1:
#print i
for i1 in x1[i]:
print i1 '''
a=0
b=0
driver=webdriver.Chrome()
driver.get(url)
#print driver.title
for i in x1:
#print i
for i1 in x1[i]:
#print i1
try:
d=driver.find_element_by_xpath(i1)
a+=1
print d.text
time.sleep(2)
driver.find_element_by_xpath(i1).click()
headx=driver.window_handles
#print headx
print '当前页面地址:\n',driver.current_url
time.sleep(1)
print i,'\n'
if len(headx)!=1:
driver.switch_to_window(headx[1])
durl= driver.current_url
print '当前页面地址:\n',durl,'\n'
if 'woyihome' in durl:
driver.close()
driver.switch_to_window(headx[0])
else:
k=1
break
elif 'localhost' in driver.current_url: print a
except :
pass
#print b
print a #driver.quit() #print '====================================================' print coun test1()
Python 通过lxml遍历html xpath的更多相关文章
- python在lxml中使用XPath语法进行#数据解析
在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...
- Python爬虫利器三之Xpath语法与lxml库的用法
前面我们介绍了 BeautifulSoup 的用法,这个已经是非常强大的库了,不过还有一些比较流行的解析库,例如 lxml,使用的是 Xpath 语法,同样是效率比较高的解析方法.如果大家对 Beau ...
- python爬虫(8)--Xpath语法与lxml库
1.XPath语法 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历.XPath 是 W3C XSLT 标准的主要元素,并且 XQuery ...
- Python爬虫教程-22-lxml-etree和xpath配合使用
Python爬虫教程-22-lxml-etree和xpath配合使用 lxml:python 的HTML/XML的解析器 官网文档:https://lxml.de/ 使用前,需要安装安 lxml 包 ...
- lxml模块(应用xpath技术)
一.lxml介绍 第三方库lxml是第一款表现出高性能特征的python xml库,天生支持Xpath1.0.XSLT1.0.定制元素类,甚至python风格的数据绑定接口.lxml是通过Cpytho ...
- Python爬虫之lxml-etree和xpath的结合使用
本篇文章给大家介绍的是Python爬虫之lxml-etree和xpath的结合使用(附案例),内容很详细,希望可以帮助到大家. lxml:python的HTML / XML的解析器 官网文档:http ...
- Python:lxml
学习自: python3解析库lxml - Py.qi - 博客园 lxml官方文档 lxml官方文档--lxml中的类.方法使用,如果需要查看某些方法的具体用法,就到这个网页下 python爬虫系列 ...
- python创建与遍历List二维列表
python创建与遍历List二维列表 觉得有用的话,欢迎一起讨论相互学习~Follow Me python 创建List二维列表 lists = [[] for i in range(3)] # 创 ...
- 【转】python 三种遍历list的方法
[转]python 三种遍历list的方法 #!/usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': list ...
随机推荐
- centos7 spark2.3.1集群搭建
1.安装jdk 2.安装scala 参照jdk的安装 3.ssh 免密码登录 4.安装hadoop 以上四步请参照 centos7 安装hadoop2.7.6(分布式) 5.安装spark 1) ...
- [Linux] 在 Ubuntu 19.10 上开启 SSH 服务并允许远程登录
在 Ubuntu 19.10 上安装 SSH,并开启服务 0.检查并确认系统当前是否已安装SSH: sudo ps -e | grep ssh 如果只有 ssh-agent 说明 ssh-server ...
- SpringBoot2.x+Redis+nginx实现session共享和负载均衡
1.创建SpringBoot项目添加依赖 <dependency> <groupId>org.springframework.session</groupId> & ...
- BJFU-216-基于链式存储结构的图书信息表的修改
#include<stdio.h> #include<stdlib.h> #define MAX 100 typedef struct Book{ double no; cha ...
- 远程登录Linux系统(使用xshell),远程上传加载文件(使用Xftp)
一.Xshell(远程登录Linux系统) 1.安装xshell 自己百度找安装包 2.连接登录 1.连接前提 需要Linux开启一个sshd的服务,监听22号端口,一般默认是开启的 查看是否开启: ...
- go 指针 通过指针修改int类型的值
指针的定义 :var p *int 取指针的值 :*p ------------------------------------------------------------------------ ...
- SonarQube安装教程与简单使用(基于Centos7,JDK1.8)
SonarQube 若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11686522.html 概念: SonarQube是一种自动 ...
- 【写法】为什么if判断中,值要倒着写
=============================================== 2019/8/27_第1次修改 ccb_warlock == ...
- [转]Go语言string,int,int64 ,float之间类型转换方法
1 正文 (1)int转string s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string i := ...
- 2019 迅雷java面试笔试题 (含面试题解析)
本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.迅雷等公司offer,岗位是Java后端开发,最终选择去了迅雷. 面试了很多家公司,感觉大部分公司考察的点都差不多 ...