Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

python - 使用lxml查找隐藏的元素(finding hidden elements using lxml)

im trying to find a way to get the 'value' text from hidden element.

(我试图找到一种方法来从隐藏的元素中获取“值”文本。)

also you need to consider that im really noob with coding and be specific with your answer :D

(您还需要考虑编码方面的问题,并具体说明您的答案:D)

im using python and lxml

(即时通讯使用python和lxml)

this is the page, its an online game so u need to have an account to view the page...

(这是页面,它是一个在线游戏,因此您需要拥有一个帐户才能查看页面...)

https://orange.e-sim.org/region.html?id=453

(https://orange.e-sim.org/region.html?id=453)

            <td colspan="6">
                <form method="post" action="travel.html" style="font-size:10px;padding-top:10px;">
                    <input type="hidden" name="countryId" id="countryId" value="76"/>
                    <input type="hidden" name="regionId" id="regionId" value="453" />
                    <div class="foundation-text-center">
                        <i class="icon-airplane" style="font-size:20px;vertical-align:middle;padding:2px;color:#3D6571;"></i>&nbsp;
                        <select id="ticketQuality" style="display:inline-block !important;padding:2px;" name="ticketQuality">
                            <option value="1">Q1 (14, -40 health)</option>
                            <option value="2">Q2 (0, -30 health)</option>
                            <option value="3">Q3 (7, -20 health)</option>
                            <option value="4">Q4 (0, -10 health)</option>
                            <option value="5">Q5 (9)</option>
                        </select>
                        <input type="submit" style="display:inline-block !important;padding:3px;font-size:12px;" class="travel button foundation-style" value="Travel" />
                    </div>          
                </form>
            </td>

i need value from countryId and regionId.

(我需要来自countryId和regionId的值。)

this is the element address im trying to use:

(这是我尝试使用的元素地址:)

ctId = tree.xpath('//div//div[2]//div[5]//div//table//tbody//tr[3]//td//form//input[1]//@value')[0]
rgnId = tree.xpath('//div//div[2]//div[5]//div//table//tbody//tr[3]//td//form//input[2]//@value')[0]

so, how can i get the countryId and regionId.

(因此,我如何获取countryId和regionId。)

  ask by Abolfazl translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import requests
from bs4 import BeautifulSoup


r = requests.get(
    'https://orange.e-sim.org/region.html?id=453')
soup = BeautifulSoup(r.text, 'html.parser')
country = []
region = []
for item in soup.findAll('input', attrs={'id': 'countryId'}):
    country.append(item.get('value'))
for item in soup.findAll('input', attrs={'id': 'regionId'}):
    region.append(item.get('value'))


for a, b in zip(country, region):
    print(f"CountryID: {a}, RegionID: {b}")

Output:

(输出:)

CountryID: 76, RegionID: 453

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...