当前位置: 移动技术网 > IT编程>脚本编程>Python > 我的第一个Python爬虫

我的第一个Python爬虫

2019年04月19日  | 移动技术网IT编程  | 我要评论

倩女幻境迷局攻略,大脑学院游戏,西湖龙井冲泡

参考了晚上很多资料

就是抓取https://m.i21st.cn/speaking/oraltraining_1.html这个网站上的英语资料 ,没事练练英语~哈哈~


# -*- coding: utf-8 -*-
import urllib2
import urllib
import re
import thread
import time


class html_model:  
      
    def __init__(self):  
        self.page = 1  
        self.pages = []  
       #self.mytool = html_tool()  
        self.enable = false  
        
  
    def getpage(self,page):  
        myurl = "https://m.i21st.cn/speaking/oraltraining_" + page +".html"
        myresponse  = urllib2.urlopen(myurl)  
        mypage = myresponse.read()  
        #encode的作用是将unicode编码转换成其他编码的字符串  
        #decode的作用是将其他编码的字符串转换成unicode编码  
        unicodepage = mypage.decode("utf-8")  
                
        # 找出所有class="content"的p标记  
        #re.s是任意匹配模式,也就是.可以匹配换行符  
        myitems = re.findall("

(.*?)",unicodepage,re.s) items = [] for item in myitems: items.append([item[0].replace("\n",""),item[1].replace("\n","")]) return items def loadpage(self): while self.enable: if len(self.pages)

(.*?)",unicodepage,re.s) temp = re.findall("(.*?)

",unicodepage,re.s) temp1=temp[0].encode("utf-8") temp2=re.findall("(.*?)

",temp1,re.s) temp3=temp2[0].decode("utf-8") #myitems = re.findall("[\n](.*?)(.*?)",temp1,re.s) temp4 =str(temp3.encode("gbk")) temp4=temp4.replace("'","") temp4=temp4.replace("
","") temp4=temp4.replace("","") temp4=temp4.replace("","") temp4=temp4.replace("&rsquo","") temp4=temp4.replace("&ldquo","") temp4=temp4.replace("&rdquo","") text=temp4.replace(";","'") #———————————————————————————————————————— # print self.mytool.replace_char(items[1]) print text myinput = raw_input() if myinput == "quit": self.enable = false break def start(self): self.enable = true page = self.page print u'正在加载中请稍候......' # 新建一个线程在后台加载段子并存储 thread.start_new_thread(self.loadpage,()) while self.enable: if self.pages: nowpage = self.pages[0] del self.pages[0] self.showpage(nowpage,page) page += 1 #----------- 程序的入口处 ----------- print u""" --------------------------------------- 程序:英语学习—爬虫 语言:python2.7 作者:xiantian 功能:按下回车依次浏览今日的英语资料 --------------------------------------- """ print u'请按下回车浏览今日新的内容:' raw_input(' ') mymodel = html_model() mymodel.start()


如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网