【问题标题】:BeautifulSoup get value from classBeautifulSoup 从课堂中获得价值
【发布时间】:2016-03-12 03:08:07
【问题描述】:

我在使用 BeautifulSoup 时遇到了问题。 这是我想做的:

对于我阅读的每个html页面中的每个表单,我都想获取“action”指向的URL。

这是我的代码:

def myfunction(path)
    from bs4 import BeautifulSoup

    #Retrieve htmlFiles from a folder
    pages = find_files(path, '.html') #as a list
    for page in pages:
        stream = open(page, "rw")
        soup = BeautifulSoup(stream, "lxml")
        formsoup = soup.find('form', attrs={"method":u"post"})
        if formsoup is not None:
           action = soup.find('form', attrs={"method":u"post"}).findAll("action") 
           print "Action is => %s\n" % action
           print ("Source: %s\ncode: %s\n\n\n\n\n" % (page, formsoup))
    stream.close()

这是我得到的结果:

Action is => []

    Source: mysource.html
    code: <form accept-charset="UTF-8" action="http://actionIshouldget.com/" id="user-login" method="post"><div><div class="form-item form-type-textfield form-item-name">
[... hidhing about ~20 lines that are useless for me]

这是我应该得到的结果:

Action is => http://actionIshouldget.com/

    Source: mysource.html
    code: <form accept-charset="UTF-8" action="http://actionIshouldget.com/" id="user-login" method="post"><div><div class="form-item form-type-textfield form-item-name">
[... hidhing about ~20 lines that are useless for me]

我没有设法使用for form in soup.find('form', attrs={"method":u"post"}) 或正则表达式...

【问题讨论】:

  • 你的 HTML 文件是什么样的?

标签: python html css regex beautifulsoup


【解决方案1】:

findAll() 将尝试在您拥有的结构中查找子元素,并在您的情况下搜索&lt;action&gt; 元素。

你试过了吗?

formsoup = soup.find('form', attrs={"method":u"post"})
formsoup['action']

【讨论】:

  • 嗯,是的,这是我测试的第一件事......我今天早上可能没有喝足够的咖啡,这很好......谢谢并抱歉没有用的话题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 2010-12-21
  • 2021-07-28
  • 2019-06-09
  • 2023-04-02
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多