Webscraping Excel初学者问题:需要对象

阿迪亚(Aditya Pednekar)

我正在开发一个网络爬虫。代码如下:

'============ Module 1 ======================

Sub test()

Dim eRow As Long
Dim ele As Object

Set sht = Sheets("Sheet1")
RowCount = 1
sht.Range("A" & RowCount) = "Title"
sht.Range("B" & RowCount) = "Company"
sht.Range("C" & RowCount) = "Location"
sht.Range("D" & RowCount) = "Description"

eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

Set objIE = CreateObject("InternetExplorer.Application")

myjobtype = InputBox("Enter type of job eg. sales, admin  ")
myzip = InputBox("Enter zipcode of area where you wish to work")

With objIE
    .Visible = False
    .navigate "http://www.jobs.com/"

Do While .Busy Or _
    .readyState <> 4
DoEvents
Loop

Set what = .document.getElementsByName("q")
what.Item(0).Value myjobtype

Set zipcode = .document.getElementsByName("where")
zipcode.Item(0).Value = myzip

'*====================================================*
.document.getElementByld("JobsButton").Click
'*=====================================================*


Do While .Busy Or _
    .readyState <> 4
DoEvents
Loop

For Each ele In .document.all

    Select Case ele.classname
    Case "Result"
    RowCount = RowCount + 1
    Case "Title"
    sht.Range("A" & RowCount) = ele.innertext
    Case "Company"
    sht.Range(B & RowCount) = ele.innertext
    Case "Location"
    sht.Range("C" & RowCount) = ele.innertext
    Case "Description”"
    sht.Range("D" & RowCount) = ele.innertext

End Select
Next ele
End With

Macro1

Set objIE = Nothing

End Sub

'======================单元2 ======================================================================================================================================================================================================================================================

Sub Macro1()

' Macrol Macro
' Formatting imported data

Columns("A:D").Select
Selection.Columns.AutoFit
With Selection
     .VerticalAlignment = xlTop
     .Orientation = 0
     .Addlndent = False
     .IndentLevel = 0
     .ShrinkToFit = False
     .ReadingOrder = xlContext
End With
Range("D1").Select
Columns("D:D").ColwmnW1dth = 50
Columns("A:D").Select
Selection.Rows.AutoFit
End Sub

'================================================= ===========

此代码的问题在于,以前有一个单击按钮的ID,但现在他们已删除了该ID。我尝试使用许多示例将===中的内容替换为许多示例,但大多数情况下都没有成功,该错误是我在本网站上使用的其他建议的“必需对象”。有人可以帮我替换===单击按钮的部分吗

该站点上按钮的HTML现在已更改为

<button type="submit" class="btn">Search Now</button>

================================================== ===========这是代码的工作方式:http : //www.youtube.com/watch?v=cSoRVZKRkvY

sam092

.document.getElementsByTagName("button")(0) 应该能够捕获搜索按钮

而且我为您做了更多的工作。之后使用以下代码.document.getElementsByTagName("button")(0).click

Set resultset = .document.getElementsByTagName("article")
RowCount = 2
For Each result In resultset
    sht.Range("A" & RowCount) = result.ChildNodes(0).innertext
    sht.Range("B" & RowCount) = result.ChildNodes(1).innertext
    sht.Range("C" & RowCount) = result.ChildNodes(2).innertext
    sht.Range("D" & RowCount) = result.ChildNodes(3).innertext
    RowCount = RowCount + 1
Next

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章