- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试让此脚本迭代 html 文件并打印出所需的结果。它一直给我这个错误。当表中只有一场“游戏”时,它可以正常工作,但如果有多个“游戏”,它就会崩溃。尝试修复它,以便它可以迭代多个游戏/ parking 票,但因此无法继续。
Traceback (most recent call last):
File "C:/Users/desktop/Desktop/tabletest.py", line 11, in <module>
for rows in table.find_all('tr'):
File "C:\Program Files\Python36\lib\site-packages\bs4\element.py", line 737, in __getattr__
self.__class__.__name__, attr))
AttributeError: 'NavigableString' object has no attribute 'find_all'
这是我的代码:
import pandas as pd
from bs4 import BeautifulSoup
import requests
import lxml.html as lh
with open("htmltabletest.html", encoding="utf-8") as f:
data = f.read()
soup = BeautifulSoup(data, 'lxml')
for table in soup.find('table', attrs={'id': 'eventSearchTable'}):
for rows in table.find_all('tr'):
cols = table.find_all('td')
empty = cols[0].get_text()
eventdate = cols[1].get_text()
eventname = cols[2].get_text()
tickslisted = cols[3].get_text()
pricerange = cols[4].get_text()
entry = (empty, eventdate, eventname, tickslisted, pricerange)
print(entry)
这是 html 文件中的内容:
<table class="dataTable st-alternateRows" id="eventSearchTable">
<thead>
<tr>
<th id="th-es-rb"><div class="dt-th"> </div></th>
<th id="th-es-ed"><div class="dt-th"><span class="th-divider"> </span>Event date<br/>Time (local)</div></th>
<th id="th-es-en"><div class="dt-th"><span class="th-divider"> </span>Event name<br/>Venue</div></th>
<th id="th-es-ti"><div class="dt-th"><span class="th-divider"> </span>Tickets<br/>listed</div></th>
<th id="th-es-pr"><div class="dt-th es-lastCell"><span class="th-divider"> </span>Price<br/>range</div></th>
</tr>
</thead>
<tbody class="" id="eventSearchTbody"><tr class="even" id="r-se-103577924">
<td class="nowrap"><input class="es-selectedEvent" id="se-103577924-check" name="selectEvent" type="radio"/></td>
<td class="nowrap" id="se-103577924-eventDateTime">Thu, 10/11/2018<br/>8:20 p.m.</td>
<td><div><a class="ellip" href="services/priceanalysis?eventId=103577924&sectionId=0" id="se-103577924-eventName" target="_blank">Philadelphia Eagles at New York Giants</a></div><div id="se-103577924-venue">MetLife Stadium, East Rutherford, NJ</div></td>
<td id="se-103577924-nrTickets">6655</td>
<td class="es-lastCell nowrap" id="se-103577924-priceRange"><span id="se-103577924-minPrice">$134.50</span> to<br/><span id="se-103577924-maxPrice">$2,222.50</span></td>
</tr><tr class="odd" id="r-se-103577925">
<td class="nowrap"><input class="es-selectedEvent" id="se-103577925-check" name="selectEvent" type="radio"/></td>
<td class="nowrap" id="se-103577925-eventDateTime">Thu, 10/11/2018<br/>8:21 p.m.</td>
<td><div><a class="ellip" href="services/priceanalysis?eventId=103577925&sectionId=0" id="se-103577925-eventName" target="_blank">PARKING PASSES ONLY Philadelphia Eagles at New York Giants</a></div><div id="se-103577925-venue">MetLife Stadium Parking Lots, East Rutherford, NJ</div></td>
<td id="se-103577925-nrTickets">929</td>
<td class="es-lastCell nowrap" id="se-103577925-priceRange"><span id="se-103577925-minPrice">$20.39</span> to<br/><span id="se-103577925-maxPrice">$3,602.50</span></td>
</tr></tbody>
</table>
最佳答案
错误在于您在表上迭代的方式,更具体地说是在以下行:
for table in soup.find('table', attrs={'id': 'eventSearchTable'}):
如果你想迭代,你应该使用find_all
。事实上,如果你看看这两个方法返回值的类型:
print(type(soup.find('table', attrs={'id': 'eventSearchTable'})))
# <class 'bs4.element.Tag'>
print(type(soup.find_all('table', attrs={'id': 'eventSearchTable'})))
# <class 'bs4.element.ResultSet'>
在第一种情况下,您有一个表,在第二种情况下,有一组表(在您的情况下仅由 1 个表组成),每个表的类型均为 bs4.element.Tag
。
因此,您有两个选择,要么使用
table = soup.find('table', attrs={'id': 'eventSearchTable'})
或
for table in soup.find_all("table", {"id":"eventSearchTable"}):
关于python - BeautifulSoup - 属性错误: 'NavigableString' object has no attribute 'find_all' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020762/
我有一些使用 BeautifulSoup 的 Python 屏幕抓取代码,这让我很头疼。对 html 的一个小改动使我的代码中断,但我不明白为什么它无法工作。这基本上是 html 在解析时的外观演示:
我想找到页面上的所有链接,此代码仅获取以http://开头的链接,但大多数链接都是https:// 我如何编辑下面的代码来找到两者? for link in soup.find_all('a',att
我正在尝试解析一个具有多个同名类的网站。我想采用第一个(如网页上所示)类(class)的元素。但是,find_all 或 find 不保留解析的顺序。下面是我对 find_all 的实现 请帮忙: i
您好,我正在尝试从网站获取一些信息。请原谅我,如果我的格式有任何错误,这是我第一次发布到 SO。 soup.find('div', {"class":"stars"}) 从这里我收到 我需要 “
我希望使用 BeautifulSoup 来解析一些 HMTL。我有一个有几行的表。我正在尝试查找满足某些条件(某些属性值)的行,并稍后在我的代码中使用该行的索引。 问题是:是否find_all()在它
假设我有这个 html 代码: html = """ Text 1 Text 2 """ 使用此代码: from bs4 import BeautifulSoup as bs soup = bs
我想提取 this 中的所有网址网页。 我使用的python代码是这个 htmlfile=urllib.urlopen("http://dubai.dubizzle.com/property-for-
当我抓取一个网页时,我总是遇到一个问题。 AttributeError: ResultSet object has no attribute 'find'. You're probably treat
我在 BeautifulSoup 中发现了一些奇怪的行为,如下面的示例所示。 import re from bs4 import BeautifulSoup html = """This has a
这是我第一次使用 BeautifulSoup,我不知道我做错了什么 Picks Bans Combined 这是我正在使用的 HTM
我有以下 xml, https://mystore.com/products-t-shirt.xml 2019-04-11T00:01:42-04:00 daily
我正在尝试抓取this page所有优惠,并想要迭代 但是page_soup.find_all("p", "white-strip")返回一个空列表 []。 到目前为止我的代码- from urlli
我正在做一些抓取并遇到了问题。 现在我的代码如下所示: pn = soup.find_all("a", {"class": "full"}) pfp = soup.find_all("td", {"c
我正在尝试查找具有 column 类的所有 p 标签。 This is a column More columns heh 我试过: soup.find_all(class_='column') 它返
我是 Python 的新手。我最近的项目是从博彩网站上抓取数据。我要抓取的是网页中的赔率信息。 这是我的代码 from urllib.request import urlopen as uReq fr
我正在使用 beautifulsoup 和 html 解析器执行抓取,并选择了我想要使用的 html 部分并将其保存为“容器”。 from urllib.request import urlopen
主要问题 我知道如何使用 find_all() 检索具有特定值属性的元素,但我找不到任何示例来说明如何检索具有多个可接受值之一的属性的元素。在我的例子中,我正在使用 DITA XML,我想检索范围属性
如何将 bs4 与带有空格的类标签的 find_all 一起使用? container = containers[0] product_container = container.find_all('
我目前正在研究网络爬虫。我希望我的代码从我抓取的所有网址中获取文本。函数 getLinks() 找到我想从中获取数据的链接并将它们放入数组中。该数组目前充满了 12 个链接,如下所示:' http:/
这个问题在这里已经有了答案: python3 print unicode to windows xp console encode cp437 (2 个答案) 关闭 7 年前。 我从 tutoria
我是一名优秀的程序员,十分优秀!