2017-05-17 2 views
0

Es gibt 2 Formulare in einer Webseite. Ich versuche, alle Formen und die damit verbundene Attribute einer Webseite verschrotten (http://demo.testfire.net/feedback.aspx) mit dem folgenden Code:Verwerfen mehr als ein Formular von der Webseite mit schönen Suppe

import bs4 as bs 
import urllib.request 

sauce = urllib.request.urlopen("http://demo.testfire.net/feedback.aspx").read() 
soup = bs.BeautifulSoup(sauce,"html.parser") 

form_count = 0 
for form_list in soup.find_all('form'): 
    form_count+=1 
    action_value = soup.find('form').get('action') 
    method_value = soup.find('form').get('method') 
    id_value = soup.find('form').get('id') 
    print(form_count, action_value, method_value, id_value) 

jedoch nur die erste Form der Seite zweimal gedruckt wird. Wie verwerfen Sie sowohl die Formulare als auch ihre Attribute? Hinweis: Die form_count variablen Schritten bis 2 (Da gibt es zwei Formen in der Seite)

Antwort

0

Sie soup.find('form') verwenden, die die erste Form gibt es auf der Seite findet statt form_list, die die aktuelle Form zurückkehrt, während Iterieren durch alle von ihnen. Ihr Code sollte wie folgt aussehen:

import bs4 as bs 
import urllib.request 

sauce = urllib.request.urlopen("http://demo.testfire.net/feedback.aspx").read() 
soup = bs.BeautifulSoup(sauce,"html.parser") 

form_count = 0 
for form_list in soup.find_all('form'): 
    form_count+=1 
    action_value = form_list.get('action') 
    method_value = form_list.get('method') 
    id_value = form_list.get('id') 
    print(form_count, action_value, method_value, id_value) 
Verwandte Themen