python自动浏览网页_使用Python进行自动网页浏览(没有类/id)

如果我理解正确的话,首先你必须在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}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值