python 百度热点新闻

from bs4 import BeautifulSoup
import requests
import pymysql
from datetime import datetime
import time
class Baidu:
    def __init__(self,url):
        self.url = url
        self.db = pymysql.connect(user='root',password='root',host='localhost',database='laravel')

    def getinfo(self):
        user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
        headers = {"user-agent":user_agent}
        baidu = requests.get(self.url,headers=headers)
        baidu.encoding = 'gbk'
        hotlist = BeautifulSoup(baidu.text,'lxml')
        list = hotlist.select("table.list-table tr")
        for new in list:
            title = new.find('a', class_='list-title')
            rank = new.find('span',{'class':['num-top','num-normal']})
            rise = new.find('span', {'class':['icon-rise', 'icon-normal', 'icon-fair','icon-fall']})
            if rise and rank and title:
                rise = rise.string
                rank = rank.string
                url = title.get('href')
                title = title.string
                self.saveData(rank,title,rise,url)
        print("--------save end-----------------")

    def saveData(self,rank,title,rise,url):
        course = self.db.cursor()
        find_sql = "select * from baidu_hot where id>0"
        course.execute(find_sql)
        result = course.rowcount
        if result>=29:
            updatesql = "update baidu_hot set title='{}', rise='{}' ,url='{}' where rank='{}'".format(title,rise,url,rank)
            try:
                print('--------------' + title + '--------------update')
                course.execute(updatesql)
                self.db.commit()
            except OSError as err:
                self.db.rollback()
                print("OS error: {0}".format(err))
        else:
            try:
                print('--------------' + title + '--------------saved')
                sql = "insert into baidu_hot (title,rank,rise,url) values('{}','{}','{}','{}')".format(title, rank, rise, url)
                course.execute(sql)
                self.db.commit()
            except OSError as err:
                self.db.rollback()
                print("OS error: {0}".format(err))


    def time(self,timscrip):
        while True:
            self.getinfo()
            time.sleep(timscrip)


hotnew = Baidu("http://top.baidu.com/buzz?b=1")
hotnew.time(120)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值