Python steam,google play,appstore评论获取[附源码]

该代码实现了一个Python爬虫,用于抓取Steam平台上特定应用的用户评论。它通过循环请求URL,利用appid和cursor参数获取不同页面的评论,解析HTML内容获取用户名、推荐情况、游戏时长和评论文本。如果请求失败,程序会尝试重新加载数据。最终将所有评论数据存储在listAllContent列表中。
摘要由CSDN通过智能技术生成
def getInitCursorValue() :
    isEnd = False
    #...
    try:
        resp = requests.get((f"https://store.steampowered.com/appreviews/{appid}?cursor={nextCursor}&language=schinese&day_range=365&review_type=all&purchase_type=all&filter=recent"),timeout=5).json()
    except Exception as e:
        print(f"request failed:请求评论失败,尝试重新拉取...{reloadDataNum}")
        resp = ""
        isEnd = True
        reloadDataNum = reloadDataNum + 1
        time.sleep(1)
    if isEnd :
        return isEnd
    reloadDataNum = 0 #reset
    print(f"request completed , data list len ={len(listAllContent)}")
    if lastedNum == len(listAllContent):
        totalEndNum = totalEndNum + 1
        print(f"request failed:请求不到更多评论...{totalEndNum}")
        if totalEndNum >= reloadDataNumMax :
            print(f"结束请求...")
            isEnd = True
            reloadDataNum = 5
            time.sleep(1)
            return isEnd
    else:
        lastedNum = len(listAllContent)
        totalEndNum = 0 
    
    cursor =  resp["cursor"] 
    cursor = cursor.replace("+","%2B")     
    nextCursor = cursor
    html = resp["html"] #本页评论数据
    soup= BeautifulSoup(html,"lxml")
    #请求成功
    #解析 用户名
    listName = []
    listRecommend = []
    listTime = []
    listComment = []
    for business in soup.find_all('div', class_="persona_name"):
        for bb in business.find_all('a') :
            listName.append(bb.string)
    #解析 是否推荐
    for business in soup.find_all('div', class_="title ellipsis"):
        listRecommend.append(business.string)
    #解析 游戏时长
    for business in soup.find_all('div', class_="hours ellipsis"):
        text1 =  business.text.replace("\r\n\t\t\t\t\t\t","")     
        text2 =  text1.replace("\t\t\t\t\t\t\t\t\t\t\t","")    
        listTime.append(text2)
    #解析 游戏评论
    for business in soup.find_all('div', class_="content"):
        text1 =  business.text.replace("\r\n\t\t\t\t\t","")     
        text2 =  text1.replace("\t\t\t\t\t\n","")     
        listComment.append(text2)
    # all data
    for number in range(0,len(listComment)) :
        list1 = []
        list1.append(listName[number])
        list1.append(listRecommend[number])
        list1.append(listTime[number])
        list1.append(listComment[number])
        listAllContent.append(list1)
    return isEnd

URL解析

请求的URL格式:https://store.steampowered.com/appreviews/{appid}?cursor={nextCursor}&language=schinese&day_range=365&review_type=all&purchase_type=all&filter=recent

appid:应用ID 

nextCursor:用来索引下一页评论的下标,初始值等于*,下一页的值从请求的页面中可以拿到,这个值的键为"cursor"。

因此通过这个URL可以一直动态的请求到所有的评论数据。

if __name__ == '__main__':
    getGameName()
    while(len(listAllContent)<(int)(maxDataSize)):
        checkExit = getInitCursorValue()
        time.sleep(0.5)
        if checkExit == True :
            if reloadDataNum >= 5 :
                print("连接超时 5 次,结束")
                break
    fun2(listAllContent)
    time.sleep(1)

测试:拉取PUBG100条评论

google play,appstore获取比较简单,实现见下面工程。

 工程链接:Ackerman1998/Craw-SteamComment (github.com)

 python版本:3.7.0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值