我正在研究一种网络爬虫,可以从各种网站拉回物品的价格。我遇到的问题是,在amazon.com上,在有多个选项(即大小)的项目上,我可以交互,并更改下拉选择选项,但我无法让网站回发并显示该选项的具体价格。我意识到这并不是一个罕见的问题,但是大多数人似乎通过使用.dispatchEvent方法而不是.fireEvent来解决他们的问题。这似乎对我不起作用。
下拉选项(仅显示前5个):
<option value="-1" id="native_size_name_-1" data-a-id="size_name_-1" selected=""> Select</option>
<option value="0,B001249LAS" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_0" data-a-id="size_name_0" data-a-html-content="44W x 29L">
44W x 29L
</option>
<option value="1,B001249KAE" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_1" data-a-id="size_name_1" data-a-html-content="44W x 30L">
44W x 30L
</option>
<option value="2,B001243XPM" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_2" data-a-id="size_name_2" data-a-html-content="44W x 32L">
44W x 32L
</option>
<option value="3,B001243XRK" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_3" data-a-id="size_name_3" data-a-html-content="44W x 34L">
44W x 34L
</option>
<option value="4,B001243Z00" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_4" data-a-id="size_name_4" data-a-html-content="46W x 29L">
46W x 29L
</option>
<option value="5,B001245XG4" class="dropdownAvailable" data-a-css-class="dropdownAvailable" id="native_size_name_5" data-a-id="size_name_5" data-a-html-content="46W x 30L">
46W x 30L
</option>
</select><span style="min-width: 0%;" tabindex="-1" id="dropdown_selected_size_name" data-a-class="aui-variation a-fastclick-disable" class="a-button a-button-dropdown aui-variation a-fastclick-disable"><span class="a-button-inner"><span class="a-button-text a-declarative" data-action="a-dropdown-button" aria-haspopup="true" role="button" tabindex="0"><span class="a-dropdown-prompt"> Select</span></span><i class="a-icon a-icon-dropdown"></i></span></span></span>Firefox向我展示了一个与下拉菜单关联的事件:
"native_dropdown_selected_size_name").onchange = function() {
TwisterNonJs.handleDropDown[0]();这是我的代码,Excel似乎可以很好地编译.dispatchEvent方法,但IE在启动时不会触发。
URLPath = http://www.amazon.com/Lee-Big-Tall-Dungarees-Carpenter-Original/dp/B001243Z00/ elementid = "priceblock_ourprice“(带引号)
Sub getPrice(URLPath As String, elementid As String)
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
navigate:
IE.navigate URLPath
Do While IE.readystate <> 4: DoEvents: Loop
Set doc = CreateObject("htmlfile")
Set doc = IE.document
If doc Is Nothing Then GoTo navigate
doc.getElementById("native_dropdown_selected_size_name").Focus
doc.getElementById("native_dropdown_selected_size_name").SelectedIndex = 2
Dim objEvent
Set objEvent = doc.createEvent("HTMLEvents")
objEvent.initEvent "change", False, True
doc.getElementById("native_dropdown_selected_size_name").dispatchEvent objEvent
Do While IE.readystate <> 4: DoEvents: Loop
pricestr = doc.getElementById(elementid).innertext发布于 2015-11-24 04:31:43
在谷歌上搜索了很多次后,我找到了答案。我在.initEvent行中使用了反向的True & False属性。我有:
objEvent.initEvent "change", False, True它需要是:
objEvent.initEvent "change", True, False希望这对将来的人有所帮助。
https://stackoverflow.com/questions/33636790
复制相似问题