【发布时间】:2017-10-16 15:57:07
【问题描述】:
一个网页中有两种形式。我正在尝试使用以下代码抓取网页 (http://demo.testfire.net/feedback.aspx) 的所有表单和相关属性:
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)
但是,只有页面的第一种形式会被打印两次。如何同时抓取表单及其属性? 注意:form_count 变量递增到 2(因为页面中有 2 个表单)
【问题讨论】:
标签: python web-scraping beautifulsoup