#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的更多相关文章

  1. python在lxml中使用XPath语法进行#数据解析

    在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...

  2. Python爬虫利器三之Xpath语法与lxml库的用法

    前面我们介绍了 BeautifulSoup 的用法,这个已经是非常强大的库了,不过还有一些比较流行的解析库,例如 lxml,使用的是 Xpath 语法,同样是效率比较高的解析方法.如果大家对 Beau ...

  3. python爬虫(8)--Xpath语法与lxml库

    1.XPath语法 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历.XPath 是 W3C XSLT 标准的主要元素,并且 XQuery ...

  4. Python爬虫教程-22-lxml-etree和xpath配合使用

    Python爬虫教程-22-lxml-etree和xpath配合使用 lxml:python 的HTML/XML的解析器 官网文档:https://lxml.de/ 使用前,需要安装安 lxml 包 ...

  5. lxml模块(应用xpath技术)

    一.lxml介绍 第三方库lxml是第一款表现出高性能特征的python xml库,天生支持Xpath1.0.XSLT1.0.定制元素类,甚至python风格的数据绑定接口.lxml是通过Cpytho ...

  6. Python爬虫之lxml-etree和xpath的结合使用

    本篇文章给大家介绍的是Python爬虫之lxml-etree和xpath的结合使用(附案例),内容很详细,希望可以帮助到大家. lxml:python的HTML / XML的解析器 官网文档:http ...

  7. Python:lxml

    学习自: python3解析库lxml - Py.qi - 博客园 lxml官方文档 lxml官方文档--lxml中的类.方法使用,如果需要查看某些方法的具体用法,就到这个网页下 python爬虫系列 ...

  8. python创建与遍历List二维列表

    python创建与遍历List二维列表 觉得有用的话,欢迎一起讨论相互学习~Follow Me python 创建List二维列表 lists = [[] for i in range(3)] # 创 ...

  9. 【转】python 三种遍历list的方法

    [转]python 三种遍历list的方法 #!/usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': list ...

随机推荐

  1. Unity3D 使用SQLite

    使用Unity操作SQLite,需要用到三个库文件,分别是Mono.Data.Sqlite.dll和System.Data.dll和Sqlite3.dll,前两个库文件可以在unity中找到,具体步骤 ...

  2. 【记录】【windows】下查看端口是否被占用并杀死该进程

    查看端口是否被占用 netstat -aon|findstr "端口号" 比如 netstat -aon|findstr "6340" 杀死该进程 taskki ...

  3. turtle1

    #画风车 from turtle import * pensize(2) for i in range(4): seth(360-i*90) fd(150) rt(90) circle(-150,45 ...

  4. 【剑指offer】数组中只出现一次的数

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 分析: 经典的异或技巧题 两个相同的数字异或的结果为0,一个数和0异或的结果是其本身,假设现在那 ...

  5. 【手写代码】计算1-n中总共有多少二进制1

    #include<bits/stdc++.h> #include<vector> using namespace std; //时间复杂度:O(N) int f(int x) ...

  6. 解决非controller使用@Autowired注解注入为null问题

    在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的s ...

  7. django+uWSGI+nginx的工作原理流程与部署过程

    django+uWSGI+nginx的工作原理流程与部署过程 一.前言 知识的分享,不应该只是展示出来,还应该解释这样做是为什么... 献给和我一样懵懂中不断汲取知识,进步的人们. 授人与鱼,不如授人 ...

  8. [python 2.x] xml.etree.ElementTree module

    XML 文件:xmlparse.xml <?xml version="1.0" encoding="UTF-8" standalone="no& ...

  9. 收藏单词TOEFL备份托福英语

    TOEFL托福词汇串讲(文本) alchemy(chem-化学)n. 炼金术 chemistry 化学 alder 赤杨树 联想:older 老人坐在赤杨树下 sloth 树懒 algae n.海藻 ...

  10. k8s与监控--k8s部署grafana6.0

    原文参考:https://segmentfault.com/a/1190000018335241