【问题标题】:Scrape from website with all same span name从具有所有相同跨度名称的网站上抓取
【发布时间】:2021-04-04 01:54:01
【问题描述】:

是否可以从this网站上抓取“点赞数”和“发帖数”并在google sheet上导入数据?

因为当我尝试时我得到空数据,因为这些数据的跨度基本上都是相同的...... 感谢您的帮助

【问题讨论】:

    标签: google-sheets google-sheets-formula google-sheets-api


    【解决方案1】:

    编辑: 由于您甚至想将该数据推送到谷歌表格并从他们那里阅读,我可以提出以下解决方案,您可以根据需要进行修改。

    首先,您需要安装 gspread 库并按照本教程 https://gspread.readthedocs.io/en/latest/oauth2.html 获取通过 api 访问 google 表格的凭据,然后按照以下更新的代码进行操作。

    你的工作表应该是这样的:

    代码:

    import requests
    import gspread
    
    headers = {'Accept': 'application/json', 'app-token': '33d57ade8c02dbc5a333db99ff9ae26a'}
    gc = gspread.service_account(filename="credentials.json")
    sh = gc.open("data")
    for rownumber,rowvalues in enumerate(sh.sheet1.get_all_values(),1):
        if len(rowvalues)==2:
            if rowvalues[1]=='':
                cookies = requests.post("https://onlyfans.com/api2/v2/init", headers=headers)
                data = requests.get(f"https://onlyfans.com/api2/v2/users/{rowvalues[0]}", headers=headers, cookies=cookies)
                if data.status_code == 200:
                    data = data.json()
                    sh.sheet1.update_cell(rownumber, 2, data["postsCount"])
            else:
                print(f"Check : {rowvalues}")
        else:
            cookies = requests.post("https://onlyfans.com/api2/v2/init", headers=headers)
            data = requests.get(f"https://onlyfans.com/api2/v2/users/{rowvalues[0]}", headers=headers, cookies=cookies)
            if data.status_code == 200:
                data = data.json()
                sh.sheet1.update_cell(rownumber, 2, data["postsCount"])
        print(f"{rownumber} Processed")
    

    运行此代码后,您会看到谷歌表格中的数据是否已更新,但在运行此脚本之前,请遵循提供的 URL,否则您最终会遇到错误。

    更新的 Gsheets:

    旧: 查看该网站的网络日志,我能够通过请求库和他们的一些 API 调用提取您想要的数据,如果需要,您可以查看 data.json() 字典以获取其他数据。 按照下面的代码。

    import requests
    headers={'Accept': 'application/json', 'app-token': '33d57ade8c02dbc5a333db99ff9ae26a'}
    cookies=requests.post("https://onlyfans.com/api2/v2/init",headers=headers)
    data=requests.get("https://onlyfans.com/api2/v2/users/elettra_pink",headers=headers,cookies=cookies)
    if data.status_code==200:
        data=data.json()
        print(f'Posts:{data["postsCount"]}\nPhotosCount:{data["photosCount"]}\nVideosCount:{data["videosCount"]}\nFavoritedCount:{data["favoritedCount"]}\nSubscribersCount:{data["subscribersCount"]}')
    

    输出:

    如果您有任何问题,请告诉我:)

    【讨论】:

    • 正是我要搜索的内容,但是……如何将其“集成”到谷歌表格中?我的意思是,假设我在 A1 中有个人资料目标链接,我如何在 A2 中有帖子编号?提前谢谢你,非常好!
    • 我已经修改了答案,您可以检查它,如果需要,请按照gspread 库进行更改。
    猜你喜欢
    • 2023-01-05
    • 1970-01-01
    • 2015-06-19
    • 2021-04-27
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多