【问题标题】:How to get tag value by tag name using web scraping in Excel sheet?如何使用 Excel 工作表中的 Web 抓取按标签名称获取标签值?
【发布时间】:2016-02-10 18:55:22
【问题描述】:

我每天对电子商务网站进行分析,我必须复制它们的价格。

我只想将网站的 URL 粘贴到 Excel 工作表中,然后从单元格中的标签名称中获取价格。网站源码如下:

<table>
<td>
Price
</td>
<td>
:
</td>
<td>
<input name="Price" type="text" value="148.0000" id="uxMSRPPrice1" style="width: 250px;" />
(In x.xx format)
</td>
</table>

我想在我的工作表中获得价格 148.0000,使用标签 name="Price" 价格是来源中的唯一标签

还有一件事,我只能从这样的 URL 获取源,包括查看源(我无法共享原始客户端 URL,所以我将其更改为 xyz):

https://www.xyz com/admin/ProductPage.aspx?ProductID=xxx

【问题讨论】:

  • 欢迎来到 Stackoverflow。您正在为一个复杂问题寻求一个完整的解决方案(从 url 打开网页,打开其源代码,查找标签,将值提取到 excel)。你的问题到底是什么?你已经尝试过实现什么目标?请注意,目前您的问题可能会因为过于宽泛而被关闭。
  • 感谢回复,我已经解决了这个问题。 (在这里问了这个问题之后)
  • 您也可以发布您的答案吗?这也将有助于其他人从中受益。否则,如果您不想发布它,请删除您的问题,而不是放弃它。

标签: excel web-scraping vba


【解决方案1】:
Public Sub GetValueFromBrowser()
' Keyboard Shortcut: Ctrl+q
Dim ie As Object
    Dim url As String
    Dim selling As String
    Dim cost As String
    url = Selection.Value
    Set ie = CreateObject("InternetExplorer.Application")

    With ie
      .Visible = 1
      .navigate url
       While .Busy Or .readyState <> 4
         DoEvents
       Wend
    End With

    Dim Doc As HTMLDocument
    Set Doc = ie.document

    selling = Trim(Doc.getElementsByName("sellingPrice")(0).Value)
    ActiveCell.Offset(0, 1).Value = selling
    cost = Trim(Doc.getElementsByName("costPrice")(0).Value)
    ActiveCell.Offset(0, 2).Value = cost
    ie.Quit
End Sub

【讨论】:

  • 将“.Visible=1”更改为“Visible=0”
猜你喜欢
  • 1970-01-01
  • 2013-05-11
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多