索引' [0]'不拉正确值?

时间:2017-08-11 01:58:58

标签: python beautifulsoup

我在拉"stoken" value时遇到问题,我注意到我的脚本似乎无论出于何种原因而无法跳过它们。 0应为5cd3840,但会返回1。为什么这个以及我需要指定什么才能获取被盗的价值?

  <form class="search" action="https://www.randomsite.com;" method="get" name="search">
        <div class="searchBox">
            <input type="hidden" name="stoken" value="5CDE3840" />
<input type="hidden" name="lang" value="1" />
            <input type="hidden" name="cl" value="alist">

              <input type="hidden" name="stoken" value="5CDE3840" />
<input type="hidden" name="lang" value="1" />
              <input type="hidden" name="cnid" value="500036" />
<input type="hidden" name="listtype" value="list" />
<input type="hidden" name="actcontrol" value="details" />

              <input type="hidden" name="cl" value="details">
              <input type="hidden" name="aid" value="15763" id="aid">
              <input type="hidden" name="anid" value="15763">
              <input type="hidden" name="parentid" value="15763">
              <input type="hidden" name="panid" value="">
                                <input type="hidden" name="fnc" value="tobasket">
                        </div>     

CODE -

s = requests.session()
url = "https://www.randomsite.com"
response = s.post(url)

soup = BeautifulSoup(response.text,"lxml")
inputs = soup.find_all("input",{"type":"hidden"})
stoken = inputs[0]["value"]
cnid = inputs[3]["value"]
aid = inputs[7]["value"]
anid = inputs[8]["value"]
parentid = inputs[9]["value"]
print("STOKEN: ", stoken)
print("CNID: ", cnid)
print("AID: ", aid)
print("ANID: ", anid)
print("PARENTID: ", parentid)

输出 -

STOKEN:  1
CNID:  500036
AID:  15763
ANID:  15763
PARENTID:  15763

1 个答案:

答案 0 :(得分:1)

您可以使用&#39;名称&#39;属性选择相关标签,即:

stoken = soup.find("input", {"name":"stoken"})['value'] 
cnid = soup.find("input", {"name":"cnid"})['value']  
aid = soup.find("input", {"name":"aid"})['value'] 
anid = soup.find("input", {"name":"anid"})['value'] 
parentid = soup.find("input", {"name":"parentid"})['value']