How to get the hidden input's value by using python?

后端 未结 1 532
终归单人心
终归单人心 2021-01-05 00:56

How can i get input value from html page

like



        
相关标签:
1条回答
  • 2021-01-05 01:35

    Using re module to parse xml or html is generally considered as bad practice. Use it only if you are responsable for the page you try to parse. If not, either your regexes are awfully complex, or your script could break if someone replaces <input type="hidden" name=.../> with <input name="..." type="hidden" .../> or almost anything else.

    BeautifulSoup is a html parser that :

    • automatically fixes minor errors (unclosed tags ...)
    • build a DOM tree
    • allows you to browse the tree, search for specific tags, with specific attributes
    • is useable with Python 2 and 3

    Unless you have good reasons not to do it, you should use it rather than re for HTML parsing.

    For example assuming that txt contains the whole page, find all hidden fields would be as simple as :

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(txt)
    hidden_tags = soup.find_all("input", type="hidden")
    for tag in hidden_tags:
        # tag.name is the name and tag.value the value, simple isn't it ?
    
    0 讨论(0)
提交回复
热议问题