2016-01-21 57 views
0

我必須模擬scrapy中的表單提交以生成頁面。Scrapy表單提交html選擇不工作

以下是形式(切小件)

<form id="" accept-charset="utf-8" method="POST" action="#"> 
<fieldset> 
<div class="select-style"> 
<select id="study-select" name=""> 
<option>Choose an area of study</option> 
<option data-tag="a1">Anthropology</option> 
<option data-tag="a2">Architecture</option> 
<option data-tag="b1">Biology</option> 
<option data-tag="b2">Botany</option> 
... 
</select> 
</div> 
</fieldset> 
</form> 

我寫在scrapy下面的代碼。我的表單xpath是正確的。我在scrapy shell中測試代碼是肯定的。

resfrom = scrapy.FormRequest.from_response(response, 
formxpath='//div[@id="field_switcher"]//form', 
formdata={'study-select':'Biology'}, 
clickdata={'type':'submit'}, method= 'POST') 

但這不起作用。我只是不能「發佈」這一點。 隨後寫resfrom.body'只是給出'study-select=Biology'。 如何「發佈」數據到scrapy中的id字段?我嘗試了很多選擇,但似乎沒有任何工作。你在我的代碼中看到了什麼問題。

+0

你能提供的URL此頁面? – alecxe

+0

謝謝。 http://www.apple.com/retail/storelist/ 我改變了一下。 :) –

+0

首先您的表單示例沒有'id'('id =「」',並且您共享的網站不包含該表單 – eLRuLL

回答

1

在你的情況下,有沒有表格提交。數據已經存在於HTML中。

下面是示例代碼組商店位置按國家:

$ scrapy shell http://www.apple.com/retail/storelist/ 
>>> from pprint import pprint 
>>> 
>>> data = {} 
>>> for country in response.css(".section-country-stores .listing"): 
...  country_id = country.xpath("@id").extract_first().replace("stores", "") 
...  data[country_id] = [" ".join(map(unicode.strip, place.xpath(".//li//text()").extract())) for place in country.css("ul")] 
... 
>>> pprint(data) 
{u'ae': [u'Abu Dhabi, Yas Mall Yas Mall Yas Island Abu Dhabi 800 04441824', 
     u'Dubai, Mall of the Emirates Mall of the Emirates Al Barsha 1 Dubai 800 04441819'], 
u'au': [u'Canberra Canberra Centre Canberra ACT 2601 (02) 6224 9500', 
     u'Bondi 213 Oxford Street Bondi Junction NSW 2022 (02) 9019 2400', 
     ... 
     ], 
... 
} 
+0

謝謝@alecxe。我可以在不提交表單的情況下獲取數據,但是如果我想提交表單,我的意思是選擇一個國家會改變頁面的顯示方式,如果我想訪問那個頁面,我可能會錯誤。表單不提交那麼爲什麼它在那裏我正在從一些幫助材料中學習scrapy –

+1

@mkike根據瀏覽器開發者工具,選擇一個國家不會發出任何網絡請求,意思是沒有表格提交.. – alecxe

+0

是的,我檢查過,所以它只是調用腳本來改變顯示權限,而這個東西不能被「模擬」? –