python爬虫——获取依据steam当前在线玩家人数排列的最热门游戏

先看一个爬虫入门的小程序——获取中国最好大学排名

import requests
from bs4 import BeautifulSoup
allUniv = []
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = 'utf-8'
        return r.text
    except:
        return ""
def fillUnivList(soup):
    data = soup.find_all('tr')
    for tr in data:
        ltd = tr.find_all('td')
        if len(ltd)==0:
            continue
        singleUniv = []
        for td in ltd:
            singleUniv.append(td.string)
        allUniv.append(singleUniv)
def printUnivList(num):
    print("{:^4}{:^10}{:^5}{:^8}{:^10}".format("排名","学校名称","省市","总分","培养规模"))
    for i in range(num):
        u=allUniv[i]
        print("{:^4}{:^10}{:^5}{:^8}{:^10}".format(u[0],u[1],u[2],u[3],u[6]))
def main():
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2020.html'
    html = getHTMLText(url)
    soup = BeautifulSoup(html, "html.parser")
    fillUnivList(soup)
    printUnivList(10)
main()

在这里插入图片描述

稍微修改一下源程序——获取某省最好大学排名

import requests
from bs4 import BeautifulSoup
allUniv = []
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = 'utf-8'
        return r.text
    except:
        return ""
def fillUnivList(soup):
    data = soup.find_all('tr')
    for tr in data:
        ltd = tr.find_all('td')
        if len(ltd)==0:
            continue
        singleUniv = []
        for td in ltd:
            singleUniv.append(td.string)
        allUniv.append(singleUniv)
def printUnivList(num):
    j = 1
    print("{1:{0}^4}{2:{0}^6}{3:{0}^10}{4:{0}^6}{5:{0}^8}{6:{0}^10}".format(chr(12288),"省内排名","全国排名","学校名称","省市","总分","培养规模"))
    for i in range(num):
        u=allUniv[i]
        if u[2] == '江西':
            print("{1:{0}^6}{2:{0}^6}{3:{0}^10}{4:{0}^6}{5:{0}^9}{6:{0}^11}".format(chr(12288),j,u[0],u[1],u[2],u[3],u[6]))
            j=j+1

def main():
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html'
    html = getHTMLText(url)
    soup = BeautifulSoup(html, "html.parser")
    fillUnivList(soup)
    printUnivList(500)
main()

在这里插入图片描述

正文:写一个获取当前在线玩家人数排列的最热门游戏的小程序

学习完上面的爬虫入门程序后的某一天,我碰巧浏览到steam网站冒出灵感:写一个获取当前在线玩家人数排列的最热门游戏的小程序。
在这里插入图片描述
按F12发现,这个网页的源码正好适合新手练习。
在这里插入图片描述
干脆动手写一写吧!

源码

import requests
from bs4 import BeautifulSoup
import csv

def getHTMLText(url):
    try:
        r=requests.get(url)
        r.raise_for_status()
        r.encoding='utf-8'
        return r.text
    except:
        return ""

def getMostPopularGamesList(steamHTMLText):
    #课本上中国最好大学排名的实例
    # 用一个singleGameData列表保存游戏当前玩家人数,今日峰值,游戏名称的数据
    #将所有游戏的singleGameData再存入一个gameList的列表
    gameList = []
    soup=BeautifulSoup(steamHTMLText)
    gameTr=soup.find_all("tr",{"class":"player_count_row"})
    for tr in gameTr:
        singleGameData=[]
        for span in tr.find_all("span",{"class":"currentServers"}):
            singleGameData.append(span.string)
        for a in tr.find_all("a",{"class":"gameLink"}):
            singleGameData.append(a.string)
        gameList.append(singleGameData)
    return gameList

def printList(gameList):
    print("依据当前玩家人数排列的最热门游戏")
    print("{1:{0}<4}{2:{0}<8}{3:{0}<10}{4:{0}<10}".format((chr(12288)),"排名","当前玩家人数","今日峰值","游戏"))
    for i in range(num):
        g=gameList[i]#获取每个游戏的数据列表
        print("{1:{0}<4}{2:{0}<8}{3:{0}<10}{4:{0}^10}".format((chr(12288)),i+1,g[0],g[1],g[2]))

if __name__ == '__main__':
    url = "https://store.steampowered.com/stats/"
    steamHTMLText = getHTMLText(url)
    gameList = getMostPopularGamesList(steamHTMLText)
    num = len(gameList)  # 网站只给出前一百的数据,因此num的值永远是100
    printList(gameList)

在这里插入图片描述

当然,steam上还有其他数据可以作为爬虫的练习。例如steam的硬软件调查等:
在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的 Python 爬虫实现,用于从北京租房信息网站上爬取租房信息并将其导入到 Excel 文件中。 首先,我们需要安装以下 Python 库: - requests:用于发送 HTTP 请求和获取网页内容。 - Beautiful Soup:用于解析 HTML 和 XML 网页内容。 - openpyxl:用于操作 Excel 文件。 可以使用 pip 命令进行安装: ``` pip install requests beautifulsoup4 openpyxl ``` 接下来,我们需要编写 Python 代码: ```python import requests from bs4 import BeautifulSoup from openpyxl import Workbook # 发送 HTTP 请求并获取网页内容 url = 'https://bj.zu.anjuke.com/fangyuan/p1/' response = requests.get(url) html = response.text # 使用 Beautiful Soup 解析网页内容 soup = BeautifulSoup(html, 'html.parser') houses = soup.select('.zu-itemmod') # 创建 Excel 文件并添加表头 wb = Workbook() ws = wb.active ws.append(['标题', '链接', '小区', '面积', '租金']) # 遍历租房信息并将其添加到 Excel 文件中 for house in houses: title = house.select('.zu-info h3 a')[0].text.strip() link = house.select('.zu-info h3 a')[0]['href'] community = house.select('.details-item')[0].text.strip() area = house.select('.details-item')[1].text.strip() price = house.select('.zu-side strong')[0].text.strip() ws.append([title, link, community, area, price]) # 保存 Excel 文件 wb.save('beijing_rent.xlsx') ``` 该爬虫程序将会从北京租房信息网站的第一页开始爬取租房信息,包括标题、链接、小区、面积和租金,并将其添加到 Excel 文件中。你可以根据需要修改代码以实现更多功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值