简体   繁体   English

无法通过BeautifulSoup从Python的输入字段中提取值

[英]Unable to extract value from input field in python via beautifulsoup

I am try to extract a value from a input field, using beautiful soup. 我尝试使用漂亮的汤从输入字段中提取值。 I have tried all that I knew of, but it always returns None 我已经尝试了所有我知道的东西,但是它总是返回None

I have tried using .text, .content and get_text() method properties, but still doesn't work. 我尝试使用.text,.content和get_text()方法属性,但仍然无法正常工作。

results = requests.post(URL, data=payload_dict);
soup = BeautifulSoup(results.content, 'html.parser');
info = soup.find(class_='mandatory airport-complete');
print(info, end="-------\n")
print(info.attrs, end="-------\n")
print (info.value, end="-------\n")
print (info.contents, end="-------\n")
print (info.get_text(), end="-------\n")

Actual output: 实际输出:

<input class="mandatory airport-complete" data-complete-min-length="4" id="segments0departCity" name="searchAir.segments[0].departCity" onfocus="departCityFocus(this)" preferredcountry="NZ" title="Enter the departure city" type="text" value="Luang Prabang (LPQ)"> </input>"-------

{'type': 'text', 'id': 'segments0departCity', 'name': 'searchAir.segments[0].departCity', 'value': 'Luang Prabang (LPQ)', 'onfocus': 'departCityFocus(this)', 'title': 'Enter the departure city', 'data-complete-min-length': '4', 'preferredcountry': 'NZ', 'class': ['mandatory', 'airport-complete']}-------

None-------

[' ']-------

Expected: "Luang Prabang (LPQ)" Actually, I am interested in only the code LPQ, but can live with the whole value, by trying some patterns in regex 预期: "Luang Prabang (LPQ)"实际上,我仅对代码LPQ感兴趣,但是可以通过尝试使用正则表达式中的某些模式来实现全部价值

Screenshot of code and output dump from jupyter notebook..since this website seems to messup some formatting. 屏幕截图来自jupyter notebook ..的代码和输出转储的屏幕快照,因为此网站似乎弄乱了某些格式。 p from juu p来自juu

'value': 'Luang Prabang (LPQ)', is in your info.attrs dict it looks like? 'value': 'Luang Prabang (LPQ)',在您的info.attrs dict中看起来像吗? input is info.attrs['value'] if I'm reading your outputs right. 如果我正确阅读您的输出,则输入为info.attrs['value']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM