TypechoJoeTheme

Dcr163的博客

统计

Python3 抓取糗事百科最新文本笑话

2019-07-21
/
0 评论
/
865 阅读
/
正在检测是否收录...
07/21
from bs4 import BeautifulSoup
import requests
#糗事百科 首页最新笑话抓取
class Downxh(object):
    def __init__(self):
        self.domain = 'https://www.qiushibaike.com'
        self.target = self.domain + '/text/'
        self.urls = []
    # 根据首页获取列表url
    def getHtml(self):
        info = requests.get(self.target)
        html = info.text
        htmlObj = BeautifulSoup(html,'html.parser')
        cHtml = htmlObj.find('div',id = 'content-left')
        html2Obj = BeautifulSoup(str(cHtml),'html.parser')
        lists = html2Obj.find_all('a',class_ = 'contentHerf')
        for item in lists:
            self.urls.append( self.domain + item.get('href') )

    # 遍历urls保存到txt文件
    def saveContent(self):
        print('--正在下载最新' + str(len(self.urls)) + '条笑话--')
        f = open('最新笑话.txt', 'w', encoding='utf-8')
        i = 1
        for url in self.urls:
            print('正在下载第:' + str(i) + '条')
            info = requests.get(url)
            html = info.text
            htmlObj = BeautifulSoup(html, 'html.parser')
            contents = htmlObj.find_all('div', class_='content')
            content = contents[0].text
            f.write(content + '\n\n')
            i = i+1
        f.close()
    # 执行抓取
    def run(self):
        self.getHtml()
        self.saveContent()
        print('下载完成')
    pass
downObj = Downxh()
# 运行
downObj.run()

如果没有:BeautifulSoup 和 requests 插件的请先用pip安装一下

pip install beautifulsoup4
pip install requests


下面是运行结果

朗读
赞(0)
版权属于:

Dcr163的博客

本文链接:

https://dcr163.cn/214.html(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. William Tardent
    2024-02-27
  2. Maryann Hamer
    2024-02-27
  3. Sanora Pantano
    2024-02-24
  4. aa
    2024-02-21
  5. Kassie Baum
    2024-01-28

标签云