Python爬取某贴吧第一页的所有帖子的标题、连接、作者,将数据储存到txt文件中

学习网络爬虫的第二个程序:
#-*- coding:utf-8 -*-

#import re
import urllib
from bs4 import BeautifulSoup
import urlparse   #处理url链接的库
import chardet #字符集检测

import sys     #解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-15: ordinal not in range(128)
reload(sys)
sys.setdefaultencoding( "utf-8" )

"""
爬取某贴吧第一页的所有帖子的标题、连接、作者,将数据储存到txt文件中
"""

def get_content(url):
	""" 获取页面源码"""
	html = urllib.urlopen(url) #获取网站页面的地址
	content = html.read()      #将页面读取到content变量中	
	html.close()	            #关闭页面
	#local = '/root/desktop/python/teiba.html'
	#urllib.urlretrieve(url,local) #将页面源码下载的本地
	#print chardet.detect(content)  #检测网页的字符集,依据网页具体内容
	return content
	
def get_author(info):
	"""
	提取帖子里面的作者  
	"""	
	soup = BeautifulSoup(info) #转换成soup对象
	all_author = soup.find_all('
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值