Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Beautifulsoup解析HTML并写入CSV-AttributeError或未解析HTML_Python_Csv_Beautifulsoup - Fatal编程技术网

Python 使用Beautifulsoup解析HTML并写入CSV-AttributeError或未解析HTML

Python 使用Beautifulsoup解析HTML并写入CSV-AttributeError或未解析HTML,python,csv,beautifulsoup,Python,Csv,Beautifulsoup,我收到一个错误,或者没有使用以下代码解析/写入任何内容: soup = BeautifulSoup(browser.page_source, 'html.parser') userinfo = soup.find_all("div", attrs={"class": "fieldWrapper"}) rows = userinfo.find_all(attrs="value") with open('testfile1.csv', 'w') as outfile: writer = c

我收到一个错误,或者没有使用以下代码解析/写入任何内容:

soup = BeautifulSoup(browser.page_source, 'html.parser')
userinfo = soup.find_all("div", attrs={"class": "fieldWrapper"})
rows = userinfo.find_all(attrs="value")

with open('testfile1.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(rows)
rows=userinfo.find_all(attrs=“value”)

AttributeError:“ResultSet”对象没有“find_all”属性

所以我尝试了一个带有print的for循环,只是为了测试它,但在程序成功运行时,它不会返回任何结果:

userinfo = soup.find_all("div", attrs={"class": "fieldWrapper"})
for row in userinfo:
    rows = row.find_all(attrs="value")
    print(rows)
这就是我试图解析的html。我正在尝试从值属性返回文本:

<div class="controlHolder">
                        <div id="usernameWrapper" class="fieldWrapper">
                            <span class="styled">Username:</span>
                            <div class="theField">
                                <input name="ctl00$cleanMainPlaceHolder$tbUsername" type="text" value="username" maxlength="16" id="ctl00_cleanMainPlaceHolder_tbUsername" disabled="disabled" tabindex="1" class="textbox longTextBox">
                                <input type="hidden" name="ctl00$cleanMainPlaceHolder$hdnUserName" id="ctl00_cleanMainPlaceHolder_hdnUserName" value="AAubrey"> 
                            </div>
                        </div>
                        <div id="fullNameWrapper" class="fieldWrapper">
                            <span class="styled">Full Name:</span>
                            <div class="theField">
                                <input name="ctl00$cleanMainPlaceHolder$tbFullName" type="text" value="Full Name" maxlength="50" id="ctl00_cleanMainPlaceHolder_tbFullName" tabindex="2" class="textbox longTextBox">
                                <input type="hidden" name="ctl00$cleanMainPlaceHolder$hdnFullName" id="ctl00_cleanMainPlaceHolder_hdnFullName" value="Anthony Aubrey">
                            </div>
                        </div>
                        <div id="emailWrapper" class="fieldWrapper">
                            <span class="styled">Email:</span>
                            <div class="theField">
                                <input name="ctl00$cleanMainPlaceHolder$tbEmail" type="text" value="email@email.com" maxlength="60" id="ctl00_cleanMainPlaceHolder_tbEmail" tabindex="3" class="textbox longTextBox">
                                <input type="hidden" name="ctl00$cleanMainPlaceHolder$hdnEmail" id="ctl00_cleanMainPlaceHolder_hdnEmail" value="aaubrey@bankatunited.com">
                                <span id="ctl00_cleanMainPlaceHolder_validateEmail" style="color:Red;display:none;">Invalid E-Mail</span>
                            </div>
                        </div>
                        <div id="commentWrapper" class="fieldWrapper">
                            <span class="styled">Comment:</span>
                            <div class="theField">
                                <textarea name="ctl00$cleanMainPlaceHolder$tbComment" rows="2" cols="20" id="ctl00_cleanMainPlaceHolder_tbComment" tabindex="4" class="textbox longTextBox"></textarea>
                                <input type="hidden" name="ctl00$cleanMainPlaceHolder$hdnComment" id="ctl00_cleanMainPlaceHolder_hdnComment">
                            </div>
                        </div>

用户名:
全名:
电邮:
无效电子邮件
评论:

您的第一个错误源于
find\u all
返回一个结果集,这或多或少是一个列表:您必须遍历
userinfo
的元素,并在这些元素上调用
find\u all

对于第二个问题,我很确定当
attrs
被传递一个字符串时,它会搜索以该字符串作为其类的元素。您提供的html不包含class
value
的元素,因此没有任何内容可以打印出来。您可以使用
.get('value')

要打印文本输入的值,应使用以下代码。 (try/except只是为了在找不到文本输入时脚本不会崩溃)


我明白你的意思了,我试着使用你提供的代码,但还是没打印出来。我试图从value=“username”value=“Full Name”value=”获取文本email@email.com“当我试图从表单中提取文本时。明白了。当我使用您提供的源HTML初始化BeautifulSoup时,上面编辑的答案将打印预期的输出。如果它仍然没有打印出任何内容,那么它可能是
浏览器。page\u source
不是您所期望的,或者您的解析器没有正确处理页面。我尝试了您编写的新版本,但仍然没有输出任何内容,我输入了
除了:print('no text found')
,只是想看看它是否会打印任何内容,但仍然没有,这似乎很奇怪,我认为你是对的,可能页面源代码有问题。我正在使用selenium来实现代码中的这一点,没有任何问题。嗯,好的。这可能是一个解析器/汤问题。
browser.page\u source
是合理的值吗?另外,当您使用示例HTML初始化BeautifulSoup时,上面的解决方案对您有效吗?上面的解决方案对示例HTML非常有效,非常感谢。我正在使用selenium导航站点,以访问所需的页面,因为访问该页面需要信息。也许我指的那页有问题<代码>浏览器。page_source应将BeautifulSoup指向正确的页面,但它似乎不是从那里解析的。
for field_wrapper in soup.find_all("div", attrs={"class": "fieldWrapper"}):
    try:
        print(field_wrapper.find("input", attrs={"type": "text"}).get('value'))
    except:
        continue