如何利用python爬虫技术将百度贴吧上面的帖子名称爬取下来
2016-06-28 10:06
639 查看
下面的程序可以将百度贴吧上的发帖标题爬取下来,源码如下:
#coding=utf-8
import urllib2
import urllib
import json
from lxml import etree
page=open('tieba.json','w')
request=urllib2.Request('http://tieba.baidu.com/f?ie=utf-8&kw=%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB&fr=search')
response=urllib2.urlopen(request)
rehtml=response.read().decode('utf-8')#这一步是模拟浏览器解码
html=etree.HTML(rehtml)
result=html.xpath('//li//a[@class="j_th_tit "]')#注意空格
for site in result:
item={}
name=site.text
item['name']=name
line = json.dumps(item,ensure_ascii=False) + '\n'
page.write(line.encode('utf-8'))#这一步是将ascii码转化成utf-8
page.close()
#coding=utf-8
import urllib2
import urllib
import json
from lxml import etree
page=open('tieba.json','w')
request=urllib2.Request('http://tieba.baidu.com/f?ie=utf-8&kw=%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB&fr=search')
response=urllib2.urlopen(request)
rehtml=response.read().decode('utf-8')#这一步是模拟浏览器解码
html=etree.HTML(rehtml)
result=html.xpath('//li//a[@class="j_th_tit "]')#注意空格
for site in result:
item={}
name=site.text
item['name']=name
line = json.dumps(item,ensure_ascii=False) + '\n'
page.write(line.encode('utf-8'))#这一步是将ascii码转化成utf-8
page.close()
相关文章推荐
- python 字典(dict)按键和值排序
- Longest Common Prefix Leetcode Python Java
- python访问MySQL
- python问题--缩进,IndentationError:expected an indented block
- Container With Most Water Leetcode Python Java
- python做考勤统计
- Python机器学习与实战笔记之朴素贝叶斯分类
- python 切片
- 简单谈谈python的反射机制
- python ctrl +d 退出
- Python实现带百分比的进度条
- Palindrome Number Leetcode Python Java
- Reverse Integer Leetcode Python Java
- python学习笔记之面向对象、基本I/O操作(一)
- [Python爬虫笔记][随意找个博客入门(一)]
- python包使用指南-创建虚拟环境
- python类库
- python中的oop面向对象基本编程(类、实例、魔术)
- python setup
- Python log code