kingcope
kingcope

Reputation: 1141

Scrape input value from html

I need to scrape an input hidden value from HTML with BeautifulSoup, I have this html form :

<form method="post" enctype="multipart/form-data" action="http://localhost/wp-admin/update.php?action=upload-plugin">
        <input type="hidden" id="_wpnonce" name="_wpnonce" value="e2e315cd8f">
        <input type="hidden" name="_wp_http_referer" value="/wp-admin/plugin-install.php?tab=upload">       
        <label class="screen-reader-text" for="pluginzip">Plugin zip file</label>
        <input type="file" id="pluginzip" name="pluginzip">
        <input type="submit" class="button" value="Install Now">
</form>

I wrote this code:

buf_pagina1 = cStringIO.StringIO()
c.setopt(c.URL, wp_url)
c.setopt(c.WRITEFUNCTION, buf_pagina1.write)
c.setopt(c.COOKIEFILE, '')
c.setopt(c.CONNECTTIMEOUT, 5)
c.setopt(c.AUTOREFERER,1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.TIMEOUT, 15)
c.setopt(c.USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36')
c.perform()
html1           = buf_pagina1.getvalue()
buf_pagina1.close()
print html1

I need to scrape value from this input:

<input type="hidden" id="_wpnonce" name="_wpnonce" value="e2e315cd8f">

Upvotes: 1

Views: 3896

Answers (1)

alecxe
alecxe

Reputation: 473833

You can find an input by id and get the value of "value" attribute. Here's an example:

from bs4 import BeautifulSoup


data = """<form method="post" enctype="multipart/form-data" action="http://localhost/wp-admin/update.php?action=upload-plugin">
        <input type="hidden" id="_wpnonce" name="_wpnonce" value="e2e315cd8f"><input type="hidden" name="_wp_http_referer" value="/wp-admin/plugin-install.php?tab=upload">     <label class="screen-reader-text" for="pluginzip">Plugin zip file</label>
        <input type="file" id="pluginzip" name="pluginzip">
        <input type="submit" class="button" value="Install Now">
    </form>"""

soup = BeautifulSoup(data)
print soup.find("input", {'id': "_wpnonce"}).attrs['value']

prints:

e2e315cd8f

Hope that helps.

Upvotes: 4

Related Questions