这一个节点以内,再继续往下挖掘:
content =each.xpath(\'div[@class=\"d_post_content_main\"]/div/cc/div[@class=\"d_post_content j_d_post_content clearfix\"]/text()\')
这样一步步得到想要的内容
3.JSON格式
网页中很多内容使用JSON来传输,我们要把内容还原出来需要使用json模块
import json
reply_info = json.loads(each.xpath(\'@data-field\')[0].replace(\'"\',\'\'))
4.Python中的多线程
多线程可以很大幅度提高软件的处理速度,可以充分利用计算机性能,不同的核处理不同的任务,并行执行,提高处理速度,使用方法如下:
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(8)
results = pool.map(spider,page)
pool.close()
pool.join()
map 这一小巧精致的函数是简捷实现 Python 程序并行化的关键。map 源于 Lisp 这类函数式编程语言。它可以通过一个序列实现两个函数之间的映射。上面的这两行代码将 page这一序列中的每个元素作为参数传递到 spyder 方法中,并将所有结果保存到 results 这一列表中。其结果大致相当于:
results = []
for page in pages:
results.append(spyder(page))
上述代码中调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束。
全部代码:
#-*-coding:utf8-*-
from lxml import etree
from multiprocessing.dummy import Pool as ThreadPool
import requests
import json
import re
import sys
\'\'\'重新运行之前请删除content.txt,因为文件操作使用追加方式,会导致内容太多。\'\'\'
def towrite(contentdict):
#f=open(\"content.txt\",\'wb\')
f.writelines(u\'回帖时间:\' + str(contentdict[\'topic_reply_time\']) + \'\\n\')
f.writelines(u\'回帖内容:\' + str(contentdict[\'topic_reply_content\']) + \'\\n\')
f.writelines(u\'回帖人:\' + contentdict[\'user_name\'] + \'\\n\\n\')
#f.close()
def spider(url):
html = requests.get(url)
#print(html.text)
html = re.sub(r\'charset=(/w*)\', \'charset=UTF-8\', html.text)
selector = etree.HTML(html)
# print(selector)
#content_field = selector.xpath(\'//div[starts-with(@class,\"l_post l_post_bright\")]\')p_content p_content_nameplate
#content_field = selector.xpath(\'//*[@id=\"j_p_postlist\"]\')
content_field = selector.xpath(\'//div[@class=\"l_post j_l_post l_post_bright \"]\')
item = {}
for each in content_field:
reply_info = json.loads(each.xpath(\'@data-field\')[0].replace(\'"\',\'\'))
author = reply_info[\'author\'][\'user_name\']
# content1 = each.xpath(\'//div[@class=\"d_post_content_main\"]\')
content = each.xpath(\'div[@class=\"d_post_content_main\"]/div/cc/div[@class=\"d_post_content j_d_post_content clearfix\"]/text()\')
reply_time = reply_info[\'content\'][\'date\']
print(\"content:{0}\".format(content))
print(\"Reply_time:{0}\".format(reply_time))
print(\"Author:{0}\".format(author))
item[\'user_name\'] = author
item[\'topic_reply_content\'] = content
item[\'topic_reply_time\'] = reply_time
towrite(item)
if __name__ == \'__main__\':
pool = ThreadPool(8)
f = open(\'content.txt\',\'a\',encoding=\'UTF-8\')
# 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()
结果如下:
回帖时间:2015-01-11 16:52
回帖内容:[\' 6和plus纠结买哪款。还有 买完新机可以让他上色吗\']
回帖人:斗已转0
回帖时间:2015-01-11 16:53
回帖内容:[\' 我现在是以贴吧高级会员的身份帮你顶贴,请注意你的态度\']
回帖人:暑假干啥
回帖时间:2015-01-11 16:57
回帖内容:[\' 我去\']
回帖人:qw518287200
回帖时间:2015-01-11 16:57
回帖内容:[\' 能教我怎么看序列号或imei号麽,大神\\uf618\']
回帖人:花颜诱朕醉
需要注意的是,极客学院附带资料的源代码是无法使用的,以上说到的几点就是我在调试过程中淌过的坑,要注意使用Chrome对要抓取的网页进行细心分析,修改xpath参数并不断试验。
+++++++明日计划++++++++++++++++
加入计时功能,测试单线程与多线程的性能差别
尝试抓取网页中的图片并保存