2013-04-30 82 views
1

我想使用lxml和cssselect從選擇下拉框中獲取/刮取數據。如何使用cssselect和lxml從選擇下拉菜單中獲取選項值?

我已經嘗試了一些東西,搜查,沒有運氣迄今網:

所以以下或類似的代碼是一個網頁,我刮:使用以下cssselect聲明

<select id="attribute501" class="required-entry super-attribute-select" name="super_attribute[501]"> 
<option value="">Choose an Option...</option> 
<option value="33">5</option> 
<option value="31">10</option> 
</select> 

css_sel_price_dd = 'html body.catalog-product-view div.wrapper div.page div.main-container div.main div.col-main div.product-view div.product-essential form div.product-shop select' 


pack_size_obj = root.cssselect(css_sel_price_dd) 
print 'This strain has a drop down select box for different pack sizes: ' 
print pack_size_obj 
pack_size = pack_size_obj[0].text_content().encode('utf-8').strip() 
print 'Pack Size: ' + pack_size 

產生以下結果:

This strain has a drop down select box for different pack sizes: 
[<SelectElement 10e77d590 name='super_attribute[501]'>] 
Pack Size: Choose an Option... 

但到目前爲止,我無法獲得訪問來自其他選項的文本值,即5,10名來自

<option value="33">5</option> 
<option value="31">10</option> 
  • 我已經嘗試添加選項的CSS路徑和不同的東西,但能「T似乎猜到語法和不能找到什麼...

仍然沒有工作 - 這裏是實際的代碼片段與URL等

import lxml.html 
import requests 
import csv 


user_agent = {'User-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17'} 
# Open a session 
s = requests.session() 

s.get('http://www.seedsman.com/en/cannabis-seeds', headers = user_agent) 

css_sel_price_dd = '.product-shop select option' 

open_seed_url = 'http://www.seedsman.com/en/amsterdam-mist-feminised-seeds' 
print 'From the following URL: ' + open_seed_url 

r = s.get(open_seed_url, headers = user_agent) 
r_encoding = r.encoding 
print 'encoding type of r:' 
print r_encoding 

myhtml = r.text # gives us a unicode string work with unicode and encode at the end when have piece of data we want 

print myhtml.encode('utf-8') 

root = lxml.html.fromstring(myhtml) 

pack_size_obj = root.cssselect(css_sel_price_dd) 
print pack_size_obj 
for opt in pack_size_obj: 
    print '{0} : {1}'.format(opt.get('value'), opt.text) 

希望有人能看到出了什麼問題?

回報:

From the following URL: http://www.seedsman.com/en/amsterdam-mist-feminised-seeds 
encoding type of r: UTF-8 
** HTML removed for brevity ** 
[<Element option at 0x1108dcbf0>] 
None : Choose an Option... 
[Finished in 3.9s] 

回答

2

您需要添加option到您的選擇,然後你會得到選項元素的列表。看看這個示例代碼段,只有HTML被簡化了一下:

src_html = """ 
<html> 
    <head></head> 
    <body> 
     <div> 
      <select id="attribute501" class="required-entry super-attribute-select" name="super_attribute[501]"> 
       <option value="">Choose an Option...</option> 
       <option value="33">5</option> 
       <option value="31">10</option> 
      </select> 
     </div> 
    </body> 
</html> 
""" 
root = html.fromstring(src_html) 

css_sel_price_dd = 'html body div select option' 
pack_size_obj = root.cssselect(css_sel_price_dd) 
for opt in pack_size_obj: 
    print '{0} : {1}'.format(opt.get('value'), opt.text) 
+0

謝謝您的回答 - 我使用python 2.7在OSX上,無法得到這個工作?!我在最後一行發現錯誤 - 錯誤聲稱是縮進錯誤,但事實並非如此。上面的代碼應該與我的python版本一起工作嗎? – 2013-04-30 14:17:14

+0

另外,當我修改我的cssselect代碼(通過添加選項)後打印列表css_sel_price_dd - 我仍然得到一個項目的列表,這就是爲什麼我認爲這是最初不工作之前,我問這個問題。那麼你能解釋一下假設上面的工作正在發生什麼,謝謝! – 2013-04-30 14:19:37

+0

應該沒有縮進錯誤,請檢查您是否正確粘貼。至於結果,你的選擇器中可能有一個錯誤,而你正在尋找別的東西而不是你正在尋找的東西。如果您可以將網址發佈到該頁面,我可以驗證它。 – andrean 2013-04-30 14:27:18

0

可以使用xpath

options = root.xpath("//select[@id='attribute501']/option") 
option_text = [option.text for option in options] 

OR

options = root.xpath("//select[@id='attribute501']/option/text()") 
相關問題