使用 lxml 查找隐藏元素
finding hidden elements using lxml
我正在尝试找到一种从隐藏元素中获取 'value' 文本的方法。
你还需要考虑到我真的是编码新手,并具体回答你的问题 :D
im 使用 python 和 lxml
这是页面,它是一个在线游戏所以你需要有一个帐户才能查看页面...
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>
<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>
我需要来自 countryId 和 regionId 的值。
这是我正在尝试使用的元素地址:
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]
所以,我怎样才能得到 countryId 和 regionId。
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}")
输出:
CountryID: 76, RegionID: 453
我正在尝试找到一种从隐藏元素中获取 'value' 文本的方法。 你还需要考虑到我真的是编码新手,并具体回答你的问题 :D
im 使用 python 和 lxml
这是页面,它是一个在线游戏所以你需要有一个帐户才能查看页面...
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>
<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>
我需要来自 countryId 和 regionId 的值。
这是我正在尝试使用的元素地址:
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]
所以,我怎样才能得到 countryId 和 regionId。
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}")
输出:
CountryID: 76, RegionID: 453