gpt4 book ai didi

python - 索引 '[0]' 未提取正确的值?

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:22 24 4
gpt4 key购买 nike

我在提取“stoken”值时遇到问题,我注意到我的脚本似乎没有计算这些行,无论出于何种原因,只是跳过它们。 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>

代码-

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

最佳答案

可以使用'name'属性来选择相关标签,即:

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']

关于python - 索引 '[0]' 未提取正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com