# coding=utf=8
from lxml import etree
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
html = '''
<!DOCTYPE html><html><headlang="en"><metacharset="UTF-8"><title>测试-常规用法</title></head><body><divid="content"><ulid="useful"><li>这是第一条信息</li><li>这是第二条信息</li><li>这是第三条信息</li></ul><ulid="useless"><li>不需要的信息1</li><li>不需要的信息2</li><li>不需要的信息3</li></ul><divid="url"><ahref="http://jikexueyuan.com">极客学院</a><ahref="http://jikexueyuan.com/course/"title="极客学院课程库">点我打开课程库</a></div></div></body></html>
'''
selector = etree.HTML(html)
# 提取文本
content = selector.xpath('//ul[@id="useful"]/li/text()')
for each in content:
print each
# 提取属性
link = selector.xpath('//a/@href')
for each in link:
print each
# 提取标题属性
title = selector.xpath('//a/@title')
print title[0]
特殊的XPath应用
#-*-coding:utf8-*-
from lxml import etree
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
html1 = '''
<!DOCTYPE html><html><headlang="en"><metacharset="UTF-8"><title></title></head><body><divid="test-1">需要的内容1</div><divid="test-2">需要的内容2</div><divid="testfault">需要的内容3</div></body></html>
'''
html2 = '''
<!DOCTYPE html><html><headlang="en"><metacharset="UTF-8"><title></title></head><body><divid="test3">
我左青龙,
<spanid="tiger">
右白虎,
<ul>上朱雀,
<li>下玄武。</li></ul>
老牛在当中,
</span>
龙头在胸口。
</div></body></html>
'''
# 测试starts-with
selector = etree.HTML(html1)
content = selector.xpath('//div[starts-with(@id,"test")]/text()')
for each in content:
print each
# 测试
selector = etree.HTML(html2)
content_1 = selector.xpath('//div[@id="test3"]/text()')
for each in content_1:
print each # 只可以提取直接文字内容,标签取不出来
data = selector.xpath('//div[@id="test3"]')[0]
info = data.xpath('string(.)')
content_2 = info.replace('\n', '').replace(' ', '')
print content_2
测试单线程和多线程
#-*-coding:utf8-*-from multiprocessing.dummy import Pool as ThreadPool
import requests
import time
defgetsource(url):
html = requests.get(url)
urls = []
# 测试单线程时间for i in range(1, 21):
newpage = 'http://tieba.baidu.com/p/3522395718?pn=' + str(i)
urls.append(newpage)
time1 = time.time()
for i in urls:
print i
getsource(i)
time2 = time.time()
print'单线程耗时:' + str(time2 - time1)
# 测试多线程时间
pool = ThreadPool(4)
time3 = time.time()
results = pool.map(getsource, urls)
pool.close()
pool.join()
time4 = time.time()
print'并行耗时:' + str(time4 - time3)
爬取百度贴吧的评论
#-*-coding:utf8-*-from lxml import etree
from multiprocessing.dummy import Pool as ThreadPool
import requests
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
'''重新运行之前请删除content.txt,因为文件操作使用追加方式,会导致内容太多。'''deftowrite(contentdict):
f.writelines(u'回帖时间:' + str(contentdict['topic_reply_time']) + '\n')
f.writelines(u'回帖内容:' + unicode(contentdict['topic_reply_content']) + '\n')
f.writelines(u'回帖人:' + contentdict['user_name'] + '\n\n')
defspider(url):
html = requests.get(url)
selector = etree.HTML(html.text)
content_field = selector.xpath('//div[@class="l_post l_post_bright "]') # 这里有bug,百度贴吧已经改了
item = {}
for each in content_field:
reply_info = json.loads(each.xpath(
'@data-field')[0].replace('"', ''))
author = reply_info['author']['user_name']
content = each.xpath(
'div[@class="d_post_content_main"]/div/cc/div[@class="d_post_content j_d_post_content "]/text()')[0]
reply_time = reply_info['content']['date']
print content
print reply_time
print author
print'hello world'
item['user_name'] = author
item['topic_reply_content'] = content
item['topic_reply_time'] = reply_time
towrite(item)
if __name__ == '__main__':
pool = ThreadPool(4)
f = open('content.txt', 'a')
page = []
for i in range(1, 21):
newpage = 'http://tieba.baidu.com/p/3522395718?pn=' + str(i)
page.append(newpage)
results = pool.map(spider, page)
pool.close()
pool.join()
f.close()