【发布时间】:2021-04-04 01:54:01
【问题描述】:
是否可以从this网站上抓取“点赞数”和“发帖数”并在google sheet上导入数据?
因为当我尝试时我得到空数据,因为这些数据的跨度基本上都是相同的...... 感谢您的帮助
【问题讨论】:
标签: google-sheets google-sheets-formula google-sheets-api
是否可以从this网站上抓取“点赞数”和“发帖数”并在google sheet上导入数据?
因为当我尝试时我得到空数据,因为这些数据的跨度基本上都是相同的...... 感谢您的帮助
【问题讨论】:
标签: google-sheets google-sheets-formula google-sheets-api
编辑: 由于您甚至想将该数据推送到谷歌表格并从他们那里阅读,我可以提出以下解决方案,您可以根据需要进行修改。
首先,您需要安装 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"]}')
输出:
如果您有任何问题,请告诉我:)
【讨论】:
gspread 库进行更改。