gpt4 book ai didi

excel - 使用 XMLHTTP 方法时等待页面加载

转载 作者:行者123 更新时间:2023-12-03 03:09:53 26 4
gpt4 key购买 nike

在以下工作代码中,我尝试导航到特定的 YouTube channel 要将视频名称放入 Excel 中..它可以工作,但部分工作正常,因为代码仅列出了大约 30 个视频

    Dim x, html As Object, ele As Object, sKeyWords As String, i As Long

With CreateObject("MSXML2.ServerXMLHTTP")
.Open "GET", "youtube channel url videos", False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send

If .Status <> 200 Then MsgBox "Problem" & vbNewLine & .Status & " - " & .statusText: Exit Sub

Set html = CreateObject("htmlfile")
html.body.innerHTML = .responseText

我怎样才能让代码加载页面的所有内容..?以便获取那里列出的所有视频。

我发现一个网站在一个表中列出了所有视频,但是对于抓取表格的部分,我无法提取视频名称,甚至无法处理表格这是我的尝试

Sub Post_Method()
Dim http As New XMLHTTP60
Dim html As New HTMLDocument
Dim htmla As Object
Dim trow As Object
Dim tcel As Object
Dim strArg As String
Dim c As Long
Dim x As Long

strArg = "inputType=1&stringInput=https%3A%2F%2Fwww.youtube.com%2Fchannel%2FUC43lrLHl4EhxaKQn2HrcJPQ&limit=100&keyType=default&customKey="

With http
.Open "POST", "https://youtube-playlist-analyzer.appspot.com/submit", False
.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
.send strArg
html.body.innerHTML = .responseText
' WriteTxtFile html.body.innerHTML
End With

Dim posts As Object, elem As Object, r As Long

'This part I can't adjust
'------------------------
Set posts = html.getElementById("container").getElementById("tableContainer").getElementById("tableData")

For Each elem In posts.Children
For Each trow In elem.Cells
c = c + 1: Cells(r + 1, c) = trow.innerText
Next trow
c = 0: r = r + 1
Next elem
'----------------------------------
Stop

End Sub

最佳答案

您可以使用该端点,然后从包含感兴趣数据的响应中提取 javascript 对象,并使用 jsonconverter.bas 进行解析。

Json 库:

我使用jsonconverter.bas。从 here 下载原始代码并添加到名为 JsonConverter 的标准模块。然后,您需要转到 VBE > 工具 > 引用 > 添加对 Microsoft 脚本运行时的引用。从复制的代码中删除顶部的属性行。

<小时/>

VBA:

Option Explicit

Public Sub GetYouTubeViews()

Dim s As String, ws As Worksheet, body As String

body = "inputType=1&stringInput=https://www.youtube.com/channel/UC43lrLHl4EhxaKQn2HrcJPQ&limit=100&keyType=default"

Set ws = ThisWorkbook.Worksheets("Sheet1")

With CreateObject("MSXML2.XMLHTTP")
.Open "POST", "https://youtube-playlist-analyzer.appspot.com/submit", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send body
s = .responseText
End With

Dim results(), r As Long, jsonSource As String
Dim json As Object, item As Object, headers()

jsonSource = GetString(s, "json_items = ", ";")

If jsonSource = "No match" Then Exit Sub

Set json = JsonConverter.ParseJson(jsonSource)

headers = Array("Title", "ViewCount")

ReDim results(1 To json.Count, 1 To UBound(headers) + 1)

For Each item In json
r = r + 1
results(r, 1) = item("title")
results(r, 2) = item("viewCount")
Next

With ws
.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
.Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
End With
End Sub

Public Function GetString(ByVal inputString As String, ByVal startPhrase As String, ByVal endPhrase As String) As String
Dim s As Long, e As Long

s = InStr(inputString, startPhrase)
If Not s > 0 Then
GetString = "No match"
Exit Function
End If

e = InStr(s + Len(startPhrase) - 1, inputString, endPhrase)

If Not e > 0 Then
GetString = "No match"
Exit Function
End If
GetString = Mid$(inputString, s + Len(startPhrase), e - (s + Len(startPhrase)))
End Function
<小时/>

结果示例:

enter image description here

<小时/>

Py:

使用 python 更加简洁

import requests, re, json ,csv

data = {
'inputType': '1',
'stringInput': 'https://www.youtube.com/channel/UC43lrLHl4EhxaKQn2HrcJPQ',
'limit': '100',
'keyType': 'default'
}

r = requests.post('https://youtube-playlist-analyzer.appspot.com/submit', data=data)
p = re.compile(r'json_items = (.*?);', re.DOTALL)
results = json.loads(p.findall(r.text)[0])

with open("data.csv", "w", encoding="utf-8-sig", newline='') as csv_file:
w = csv.writer(csv_file, delimiter = ",", quoting=csv.QUOTE_MINIMAL) #change this for locale
w.writerow(['Title','ViewCount'])
for item in results:
w.writerow([item['title'], item['viewCount']])

关于excel - 使用 XMLHTTP 方法时等待页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com