py爬虫代码案例(仅用于学习用途)

import requests
from bs4 import BeautifulSoup

def get_baidu_hot():
    url = 'https://top.baidu.com/board?tab=realtime&sa=fyb_realtime_31065'
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    hot_list = soup.select('.category-wrap_iQLoo')
    hot_data = []
    for i in range(31):
        hot_title = hot_list[i].select_one('.c-single-text-ellipsis').text
        hot_num = hot_list[i].select_one('.hot-index_1Bl1a').text
        image_url = hot_list[i].select_one('.hot-desc_1m_jR > a').get('href')
        content = hot_list[i].select_one('.hot-desc_1m_jR').text
        hot_data.append({
            'title': hot_title,
            'num': hot_num,
            'image_url': image_url,
            'content': content
        })
    return hot_data


def main():
    hot_list = get_baidu_hot()
    for hot in hot_list:
        print(hot['title'], '-', hot['num'], '-', hot['content'], '-', hot['image_url'])

if __name__ == '__main__':
    main()

一、pip 安装 requests和BeautifulSoup模块,入pip install requests和pip install beautifulsoup4;

二、拿到上述代码,并执行

本次是对python爬虫的初步认知和学习,还有这个是咨询chatgpt加上自己搜的百度热搜,纯自己手写,非版权问题,仅用于学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值