python 爬取taptap热门榜

本文介绍了使用Python结合mitmproxy和Appium工具抓取TapTap热门榜的流程。作者建议将mitmproxy服务与Appium操作分开处理,以提高效率,并指出通过API直接获取数据更便捷。
摘要由CSDN通过智能技术生成
  1. 用到工具有mitmproxy 、appium
  2. 热门榜页面
    在这里插入图片描述
    3.抓取到的数据

在这里插入图片描述

from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import threading
import csv
import json
import time

def appium():
    desired_caps = {
   }
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '10'
    desired_caps['deviceName'] = 'NX629J'
    desired_caps['appPackage'] = 'com.taptap'
    desired_caps['appActivity'] = 'com.play.taptap.ui.SplashAct'
    desired_caps['noReset'] = True

    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    paihang = WebDriverWait(driver,60).until(
        lambda d: d.find_element_by_xpath('//*[@text="排行"]')
    )

    paihang.click
要使用Python爬取Taptap论坛数据,可以按照以下步骤进行: 1. 安装必要的Python库:requests、BeautifulSoup和pandas。可以使用pip命令来安装这些库。 2. 找到需要爬取的Taptap论坛页面的URL。例如,我们可以爬取“神都夜行录”游戏的论坛页面:https://www.taptap.com/app/1369/topic。 3. 使用requests库发送HTTP请求,获取网页HTML代码。可以使用get()方法来发送GET请求,然后使用.text属性获取HTML代码。 4. 使用BeautifulSoup库解析HTML代码,提取需要的数据。可以使用find()或find_all()方法来查找HTML标签,然后使用.text属性获取标签的文本内容。 5. 将提取的数据存储到CSV文件中。可以使用pandas库创建DataFrame对象,然后使用to_csv()方法将数据保存为CSV文件。 以下是一个示例代码,可以爬取“神都夜行录”游戏的论坛页面,并将发帖人、发帖时间和帖子内容保存到CSV文件中: ```python import requests from bs4 import BeautifulSoup import pandas as pd url = 'https://www.taptap.com/app/1369/topic' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') posts = [] for post in soup.find_all('div', class_='topic-item'): author = post.find('div', class_='author-name').text.strip() time = post.find('span', class_='created-at').text.strip() content = post.find('div', class_='topic-item-body').text.strip() posts.append({'author': author, 'time': time, 'content': content}) df = pd.DataFrame(posts) df.to_csv('shendu.csv', index=False) ``` 运行以上代码后,将会在当前目录下生成一个名为“shendu.csv”的CSV文件,其中包含了论坛页面中所有帖子的发帖人、发帖时间和帖子内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值