简体   繁体   中英

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

I have tried using .text, .content and get_text() method properties, but still doesn't work.

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

Screenshot of code and output dump from jupyter notebook..since this website seems to messup some formatting. p from juu

'value': 'Luang Prabang (LPQ)', is in your info.attrs dict it looks like? input is info.attrs['value'] if I'm reading your outputs right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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