解析HTML和使用Beautifulsoup写入CSV - AttributeError的或没有HTML被解析
问题描述:
我要么收到错误或没有被解析/用下面的代码写成:解析HTML和使用Beautifulsoup写入CSV - AttributeError的或没有HTML被解析
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)
行= userinfo.find_all(ATTRS =“值”)
AttributeError的:“结果集”对象有没有属性“find_all”
所以我尝试了用打印循环只是为了测试它,但在程序成功运行不返回任何内容:
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 protected]" maxlength="60" id="ctl00_cleanMainPlaceHolder_tbEmail" tabindex="3" class="textbox longTextBox">
<input type="hidden" name="ctl00$cleanMainPlaceHolder$hdnEmail" id="ctl00_cleanMainPlaceHolder_hdnEmail" value="[email protected]">
<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_all
返回ResultSet,这或多或少是一个列表茎:你必须通过userinfo
的元素进行迭代请致电find_all
。
对于你的第二个问题,我很肯定当attrs
传递一个字符串时,它搜索该字符串作为它的类的元素。您提供的html不包含类value
的元素,所以没有任何内容可以打印出来。您可以使用.get('value')
访问元素的值要打印出文本输入的值,应该使用以下代码。 (在try /除了仅仅是如此,如果没有找到一个文本输入脚本不会崩溃)
for field_wrapper in soup.find_all("div", attrs={"class": "fieldWrapper"}):
try:
print(field_wrapper.find("input", attrs={"type": "text"}).get('value'))
except:
continue
我明白你的意思,我尝试使用您提供却又不打印输出的代码。我试图从value =“username”value =“Full Name”value =“[email protected]”中获取文本,因为我试图从表单中拉出文本。 – nvachhan
Gotcha。当我使用您提供的源HTML初始化BeautifulSoup时,我上面编辑的答案打印了预期的输出。如果它仍然没有打印出任何东西,它可能的'browser.page_source'不是你期望的,或者你的解析器不能正确处理页面。 – lanceg
我试了一下你写的新版本,但还是一无所有,我把'print:'('no text found')'看看它是否会打印任何东西,但仍然没有,这似乎很奇怪,我认为你是对的,也许是页面源的错误。我正在使用硒来代码中的这一点,没有问题。 – nvachhan