美文网首页
python 爬取糗事百科

python 爬取糗事百科

作者: 有苦向瓜诉说 | 来源:发表于2017-03-23 20:50 被阅读0次
抓取糗事百科的笑话哈哈

#面向过程版本
import requests
import re
from bs4 import BeautifulSoup


def getHTMLText(url):
    try:
        headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}
        r=requests.get(url,timeout=30,headers=headers)
        r.raise_for_status()
        r.encoding='utf-8'
        return r.text
    except:
        raise

def parseHTML(ilt,html):
    soup=BeautifulSoup(html,'html.parser')
    articleDiv=soup.find_all('div',attrs={'class':'article block untagged mb15'})
    for item in articleDiv:
        try:    
            title=item.h2.string
            article=item.span.get_text()
            joy=item.find('span',attrs={'class':'stats-vote'}).get_text()
            comment=item.find('a',attrs={'class':'qiushi_comments'}).get_text()
            ilt.append([title,article,joy,comment])
        except:
            continue
        

def printArticle(ilt):
    pattern=u'{0}\n{1}\n{2}{3}\n\n'   #将编码设置为Unicode编码,好像很好使
    for every in ilt:
        print(pattern.format(every[0],every[1],every[2],every[3]))
        with open('joke.txt','a',encoding='utf-8') as f:
            f.write(every[1]+'\n'*2)

def main():
    start_url='http://www.qiushibaike.com/hot/page/'
    pageNumber=1
    url=start_url+str(pageNumber)
    html=getHTMLText(url)
    #print(html)
    ilt=[]
    parseHTML(ilt,html)
    printArticle(ilt)

main()



#用类和实例来做
import re
import requests
from bs4 import BeautifulSoup

class QSBK(object):

    def __init__(self):
        self.pageNumber=2
        self.start_url='http://www.qiushibaike.com/hot/page/'
        self.url=self.start_url+str(self.pageNumber)
        self.ilt=[]
        self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}

    def getHTMLText(self):
        try:
            r=requests.get(self.url,timeout=20,headers=self.headers)
            r.raise_for_status()
            r.encoding='utf-8'
            return r.text
        except:
            return 'ERROR'

    def parsePage(self):
        soup=BeautifulSoup(self.getHTMLText(),'html.parser')
        articleDiv=soup.find_all('div',attrs={'class':'article block untagged mb15'})
        for item in articleDiv:
            try:    
                title=item.h2.string
                article=item.span.get_text()
                joy=item.find('span',attrs={'class':'stats-vote'}).get_text()
                comment=item.find('a',attrs={'class':'qiushi_comments'}).get_text()
                self.ilt.append([title,article,joy,comment])
            except:
                continue

    def printJoke(self):
        pattern=u'{0}\n{1}\n{2}{3}\n\n'   #将编码设置为Unicode编码,好像很好使
        for every in self.ilt:
            print(pattern.format(every[0],every[1],every[2],every[3]))
            with open('joke.txt','a',encoding='utf-8') as f:
                f.write(every[1]+'\n'*2)

spider=QSBK()
spider.getHTMLText()
spider.parsePage()
spider.printJoke()

相关文章

  • nice,64个python爬虫入门项目,学会轻轻松松爬取资源

    爬虫在手,资源我有,看完这些,你还敢说你不会爬吗?(滑稽) 1.爬取糗事百科 2.爬取妹子图 3.Python ...

  • python 3  爬糗事百科

    python 3 爬糗事百科(来源Python爬虫学习,实战一糗事百科(2017/7/21更新)) 关于head...

  • 爬虫项目

    64个爬虫项目链接 1.爬取糗事百科 2.爬取妹子图 3.Python 岗位分析报告 4.Selenium介绍 5...

  • Python爬虫实战

    注:采转归档,自己学习查询使用 Python爬虫实战(1):爬取糗事百科段子Python爬虫实战(2):百度贴吧帖...

  • Python爬虫(十七)_糗事百科案例

    糗事百科实例 爬取糗事百科段子,假设页面的URL是: http://www.qiushibaike.com/8hr...

  • Python爬虫教程一爬取糗事百科段子

    这次为大家带来,Python爬取糗事百科的小段子的例子。 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一...

  • Python 学习——每天写点小东西-1

    最近开始学习python,这里就作为学习记录,记录自己的python之路。本条爬虫爬取的是糗事百科的24小时热门里...

  • python爬虫

    1、爬取糗事百科 代码: 2、爬取淘宝手机信息 代码: 3、爬取中国大学排名 代码: 4、爬取豆瓣top250 代码:

  • python爬取糗事百科

    闲来无事,找点段子一乐呵,就逛到糗事百科,这次爬取没有什么难度,唯一值得说道的是增加了一点点的代码健壮性。 其中类...

  • python爬取糗事百科

    以下使用面向过程版的代码 面向对象版

网友评论

      本文标题:python 爬取糗事百科

      本文链接:https://www.haomeiwen.com/subject/jbwtottx.html