从下拉列表中选择值并将其隐藏为

时间:2016-12-30 07:23:41

标签: javascript python select beautifulsoup html-parsing

因此,我正在学习美丽的汤,我正在试图找出如何选择下拉值,然后将其隐藏为相关值。

我使用的链接是http://www.nakedcph.com/commodity/4876-nike-sportswear-air-max-95.

This is a html code I am referencing to

我正在尝试从商品展示形式大小中选择值,在选择它之后,我试图拉

  

name =“item_pid”id =“commodity-show-form-item_pid”value

我通过选择尺寸并使用inspect元素更改输入类型来手动测试它。它奏效了。但是当我尝试在python上进行时,我得到了

  

AttributeError:'NoneType'对象没有属性'findAll'

这是我的代码

import requests
import re
import time
import timeit
from bs4 import BeautifulSoup as bs

session = requests.session()
response = session.get('http://www.nakedcph.com/commodity/4876-nike-sportswear-air-max-95')
soup = bs(response.text, 'html.parser')
select = soup.find('select', id="commodity-show-form-size")
option_tags = select.findAll('option')
print option_tags

我被困在这一小时。请帮忙 每个人都建议我通过javascript来做。有人可以为我提供一些例子吗?

2 个答案:

答案 0 :(得分:0)

如果您的网页是javascript呈现的,您可以尝试这样的内容:

*.domain.com

答案 1 :(得分:0)

您想要的数据是由JavaScript生成的,您可以在html代码中找到它:

import re
text = '''$(document).ready(function() {
    $('#commodity-show-thumbnails').bxSlider({ mode: 'vertical', auto: false, controls: true, pager: false, minSlides: 4, maxSlides: 4, moveSlides: 1, slideWidth: 250 });
    itemSelector('commodity-show-form', 'commodity-show-addcart-submit', [['color', 'Choose color'], ['size', 'Choose size']], { "39757": { "params": ["White/Pure Platinum", "35,5"]}, "39758": { "params": ["White/Pure Platinum", "36"]}, "39759": { "params": ["White/Pure Platinum", "36,5"]}, "39760": { "params": ["White/Pure Platinum", "37,5"]}, "39761": { "params": ["White/Pure Platinum", "38"]}, "39762": { "params": ["White/Pure Platinum", "38,5"]}, "39763": { "params": ["White/Pure Platinum", "39"]}, "39764": { "params": ["White/Pure Platinum", "40"]}, "39765": { "params": ["White/Pure Platinum", "40,5"]}, "39766": { "params": ["White/Pure Platinum", "41"]}, "39767": { "params": ["White/Pure Platinum", "42"]} }, [39757,39758,39759,39760,39761,39762,39763,39764,39765,39766,39767], 'main-cart', 'commodity-show-image');
});'''

res = re.findall(r'{ "params": (.+?)}', text)  # get text format

to_list = [eval(i) for i in res] # text to python object list

print(to_list)

出:

[['White/Pure Platinum', '35,5'], ['White/Pure Platinum', '36'], ['White/Pure Platinum', '36,5'], ['White/Pure Platinum', '37,5'], ['White/Pure Platinum', '38'], ['White/Pure Platinum', '38,5'], ['White/Pure Platinum', '39'], ['White/Pure Platinum', '40'], ['White/Pure Platinum', '40,5'], ['White/Pure Platinum', '41'], ['White/Pure Platinum', '42']]