Skip to content

Commit 246feb5

Browse files
committed
lxml抓取网页
1 parent 5ec8fab commit 246feb5

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

htmlparse_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
import urllib
3+
from lxml import etree
4+
5+
6+
# url = 'https://www.python.org/events/python-events/'
7+
# f_handle = urllib.urlopen(url)
8+
# data = f_handle.read()
9+
# html = etree.HTML(data)
10+
# nodes = html.xpath('//ul[@class="list-recent-events menu"]/li')
11+
# for node in nodes:
12+
# time = node.xpath('p/time/text()')
13+
# zt = node.xpath('h3[@class="event-title"]/a/text()')
14+
# address = node.xpath('p/span[@class="event-location"]/text()')
15+
16+
# data = {'time':time,'zt':zt,'address':address}
17+
# print data
18+
19+
20+
# url = 'https://docs.python.org/3/faq/'
21+
# fHandle = urllib.urlopen(url)
22+
# result = fHandle.read()
23+
# html = etree.HTML(result)
24+
# nodes = html.xpath('//div[@class="toctree-wrapper compound"]/ul/li')
25+
# L = []
26+
# for node in nodes:
27+
# L.append(node.xpath('a/text()')[0])
28+
# print L
29+
30+
31+
url = "http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=a&rsv_"
32+
fhandle = urllib.urlopen(url)
33+
result = fhandle.read()
34+
html = etree.HTML(result)
35+
nodes = html.xpath('//div[@id="content_left"]/div')
36+
for node in nodes:
37+
print node.xpath('h3/a/text()')
38+
print len(nodes)

0 commit comments

Comments
 (0)