从具有相同跨度名称的网站上抓取
Scrape from website with all same span name
是否可以从 this 网站上抓取“赞数”和“post 数”并在 google sheet 上导入数据?
因为当我尝试时,我得到的是空数据,因为这些数据的跨度基本上都是相同的……
感谢帮助
已编辑:
因为您甚至想将该数据推送到 google sheet 并从他们那里读取回来,所以我可以提出以下解决方案,您可以根据需要进行修改。
首先,您需要安装 gspread 库并按照本教程 https://gspread.readthedocs.io/en/latest/oauth2.html 获取凭据以通过 api 访问 google sheets,然后按照以下步骤操作更新代码。
你的sheet应该是这样的:
代码:
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")
一旦你 运行 这段代码,你会看到数据已经在 google sheet 秒内更新,但在 运行 执行此脚本之前遵循 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"]}')
输出:
如果您有任何问题,请告诉我:)
是否可以从 this 网站上抓取“赞数”和“post 数”并在 google sheet 上导入数据?
因为当我尝试时,我得到的是空数据,因为这些数据的跨度基本上都是相同的…… 感谢帮助
已编辑: 因为您甚至想将该数据推送到 google sheet 并从他们那里读取回来,所以我可以提出以下解决方案,您可以根据需要进行修改。
首先,您需要安装 gspread 库并按照本教程 https://gspread.readthedocs.io/en/latest/oauth2.html 获取凭据以通过 api 访问 google sheets,然后按照以下步骤操作更新代码。
你的sheet应该是这样的:
代码:
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")
一旦你 运行 这段代码,你会看到数据已经在 google sheet 秒内更新,但在 运行 执行此脚本之前遵循 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"]}')
输出:
如果您有任何问题,请告诉我:)