python爬虫 爬取python高级教程到单独的txt文件

根据视频教程学习的,由于时间久了,不知道看的那个教程了。。。
入口:http://www.pythontab.com/html/pythonjichu/
下载python中文社区教程:
1.获取页面源码
2.找到标题跟内容所对应的表签
3.把标题和所对应的内容放一起
4.如何打印文章内容
5.打印标题
6.写入文件     文件名为标题      open函数
 
#_*_ coding:utf-8 _*_
import urllib2
from bs4 import BeautifulSoup
from spyder.utils.help.conf import html_context

url='http://www.pythontab.com/html/pythonhexinbiancheng/'
url_list = [url]  #链接放入列表,多页的效果
for i in range(2,21):
    #url_list.append('http://www.pythontab.com/html/pythonhexinbiancheng/%s.html'%i)
    url_list.append('http://www.pythontab.com/html/pythonhexinbiancheng/{}.html'.format(i))


source_list = []#存放标题和文字
for j in url_list:
    request = urllib2.urlopen(j)#打开网址
    html = request.read() #获取所有源码
    #print html
    soup = BeautifulSoup(html,'html.parser')#解析方式
    titles = soup.select('#catlist > li > a')#select:css选择器  从所找内容的最近的一个具有id值的属性开始寻找
    #print titles
    #获取内容,获取到a标签的超链接
    links = soup.select('#catlist > li > a')#和titles碰巧一样
    #print links
    for title,link in zip(titles,links):
        data={
            "title" : title.get_text(),#获取标题文本
            "link" : link.get('href')#获取文章的超链接
            }
        #print data     #内容+标题             中文在可迭代对象里面就是Unicode编码
        source_list.append(data)#把data追加到空列表中
    
    #获取文章内容
    for l in source_list:    
        request = urllib2.urlopen(l['link'])#打开连接
        html_cont = request.read()#获取源码
        soup_cont = BeautifulSoup(html_cont,'html.parser',from_encoding="utf-8")
        text_p = soup_cont.select('div.content > p')#查找到内容
        #print text_p#内容
        text = []#放置文章内容
        for t in text_p:
            #print t
            #print t.get_text()
            text.append(t.get_text().encode('utf-8'))#追加到空列表中
    
        #标题
        title_text = l['title']#css选择器获取文本内容
        title_text = title_text.replace('*','').replace('/','or').replace('"',' ').replace('?','wenhao').replace(':',' ')
        #print title_text  #打印标题
        
        #写入文件
        #open("路径",'模式')
        with open("F:/reptile_Python/python_spider/download/%s.txt" %title_text ,'wb') as f:  #wb:以二进制模式写
            for a in text:
                f.write(a)#写入
     
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值