使用xpath多线程爬取百度贴吧内容

#encoing=utf-8
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,因为文件操作使用追加方式,会导致内容太多。''' def towrite(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') def spider(url):
html = requests.get(url)
selector = etree.HTML(html.text)
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('&quot',''))
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 clearfix"]/text()')[0]
reply_time = reply_info['content']['date']
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,10):
newpage = 'http://tieba.baidu.com/p/3522395718?pn=' + str(i)
page.append(newpage) results = pool.map(spider, page)
pool.close()
pool.join()
f.close()

爬虫神器xpath的用法(四)的更多相关文章

  1. 爬虫神器xpath的用法(三)

    xpath的多线程爬虫 #encoding=utf-8 ''' pool = Pool(4) cpu的核数为4核 results = pool.map(爬取函数,网址列表) ''' from mult ...

  2. 爬虫神器xpath的用法(一)

    1.如果你没有安装lxml,请运行pip install lxml或者easy_install lxml安装,如果在安装过程中失败的话, 是因为lxml需要依赖某些库文件,具体可以问下度娘,这里不再赘 ...

  3. 爬虫神器xpath的用法(二)

    爬取网页内容的时候,往往网页标签比较复杂,对于这种情况,需要用xpath的starts-with和string(.)功能属性来处理,具体看事例 #encoding=utf-8 from lxml im ...

  4. 爬虫神器XPath,程序员带你免费获取周星驰等明星热门电影

    本教程由"做全栈攻城狮"原创首发,本人大学生一枚平时还需要上课,但尽量每日更新文章教程.一方面把我所习得的知识分享出来,希望能对初学者有所帮助.另一方面总结自己所学,以备以后查看. ...

  5. 【爬虫】Xpath高级用法

    xpath速度比较快,是爬虫在网页定位中的较优选择,但是很多网页前端代码混乱难以定位,而学习定位也较为不易(主要是全面的教程较少),这里列出一点编程过程中可能有用的东西,欢迎共同学习批评指正.试验环境 ...

  6. python爬虫神器PyQuery的使用方法

    你是否觉得 XPath 的用法多少有点晦涩难记呢? 你是否觉得 BeautifulSoup 的语法多少有些悭吝难懂呢? 你是否甚至还在苦苦研究正则表达式却因为少些了一个点而抓狂呢? 你是否已经有了一些 ...

  7. xpath相关用法及技巧

    本节讲解网页解析神器----XPath lxml下载 xpath基本用法 xpath插件 Xpath及XML路径语言,它是一门在XML文档查找信息的语言. 一:lxml下载以及安装 首先需要解决lxm ...

  8. python爬虫---selenium库的用法

    python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...

  9. python xpath 基本用法

    转自:http://www.pythoner.cn/home/blog/python-xpath-basic-usage/ Pyer发现 业界资讯 相册 第7期:Pythoner技术交流沙龙 关于我们 ...

随机推荐

  1. LoadRunner中文乱码问题解决方案

    一下内容纯属网上方法集合: 我用loadrunner录制,脚本里的乱码一直没有解决.看到网上很多贴子.采用的方法:1.第一步:去lr 的vugen的Tools -> Recoding Optio ...

  2. linux中压缩与解压缩命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...

  3. C#键盘钩子 鼠标钩子

    最新对C#模拟键盘按键,鼠标操作产生了兴趣.特从网上收集了一些常用的API用来调用键盘,鼠标操作. class Win32API { #region DLL导入 /// <summary> ...

  4. 阿里巴巴Json工具-Fastjson讲解

    Fastjson是阿里巴巴公司开源的速度最快的Json和对象转换工具,一个Java语言编写的JSON处理器. 1.遵循http://json.org标准,为其官方网站收录的参考实现之一.2.功能qia ...

  5. upgrade-php-5-1-to-php-5-3-using-yum-on-centos

    wget -q -O - http://www.atomicorp.com/installers/atomic | shyum upgrade phpyum -y remove atomic-rele ...

  6. Points on cycle

    Description There is a cycle with its center on the origin. Now give you a point on the cycle, you a ...

  7. 简单DP(51nod 1092)

    题目:回文字符串 思路:找准状态以及决策,就可以了: 形如:E[i,j]=opt{D[i-1,j]+xi,D[i,j-1]+yj,D[i-1][j-1]+zij} (最长公共子序列) 变形即可: dp ...

  8. JavaScript-遍历数组

    遍历数组:依次访问数组中每个元素 for(var i=0; i<arr.length;i++){ arr[i] //当前数组 } <!DOCTYPE html> <html&g ...

  9. 深入浅出话VC++(1)——Windows程序内部运行机制

    一.引言 要想熟练掌握Windows应用程序的开发,首先需要理解Windows平台下程序运行的内部机制,然而在.NET平台下,创建一个Windows桌面程序,只需要简单地选择Windows窗体应用程序 ...

  10. iOS设置UISearchBar光标的颜色

    [[UISearchBar appearance] setTintColor:[UIColor blackColor]];