如果我理解正确的话,首先你必须在Excel文件中列出用户列表,我没有,所以在我的例子中,我使用这段代码得到了前25名,并将其保存到xlsx文件中:from openpyxl import load_workbook, Workbook
from lxml.html import fromstring
import requests
def get_number_of_views_and_subscriptions(socialblade_url="https://socialblade.com/youtube/"):
"""Function returns account names, account urls, number of subscribers and number of views
from socialblade web-site using requests and xpath"""
request = requests.get(socialblade_url)
tree = fromstring(request.content)
account_names = tree.xpath("/html/body/div[9]/div[1]/div/div[3]/a/text()")
account_urls = ["https://socialblade.com" + _ for _ in tree.xpath("/html/body/div[9]/div[1]/div/div[3]/a/@href")]
subscribers = tree.xpath("/html/body/div[9]/div[1]/div/div[5]/text()")
views = tree.xpath("/html/body/div[9]/div[1]/div/div[6]/text()")
data = zip(account_names, account_urls, subscribers, views)
return data
def writing_to_excel(file_path="users_data.xlsx", data=get_number_of_views_and_subscriptions()):
"""Function writes data of type ["account names", "account urls", "number of subscribers", "number of views"]
to an xlsx file"""
workbook = Workbook()
worksheet = workbook.create_sheet("Socialblade", 0)
worksheet.append(["account names", "account urls", "number of subscribers", "number of views"])
for item in data:
worksheet.append(item)
workbook.save(file_path)
下一步是获取链接和获取信息,我将使用以下代码执行此操作:
^{pr2}$