我是Python新手,一般来说对编程相当陌生。我正在尝试编写一个脚本,它使用BeautifulSoup来解析任何红色文本的https://www.state.nj.us/mvc/。我看到的表格是相对简单的HTML:
<html>
<body>
<div class="alert alert-warning alert-dismissable" role="alert">
<div class="table-responsive">
<table class="table table-sm" align="center" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td width="24%">
<strong>
<font color="red">Bakers Basin</font>
</strong>
</td>
<td width="24%">
<strong>Oakland</strong>
</td>
...
...
...
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
从上面我想找到贝克盆地,但不是奥克兰,例如
以下是我编写的Python(改编自自学程序员Cory Althoff,2017年,三角形连接LCC):
import urllib.request
from bs4 import BeautifulSoup
class Scraper:
def __init__(self, site):
self.site = site
def scrape(self):
r = urllib.request.urlopen(self.site)
html = r.read()
parser = "html.parser"
soup = BeautifulSoup(html, parser)
tabledmv = soup.find_all("font color=\"red\"")
for tag in tabledmv:
print("\n" + tabledmv.get_text())
website = "https://www.state.nj.us/mvc/"
Scraper(website).scrape()
我似乎在这里遗漏了一些东西,因为我似乎无法把这些东西从桌子上刮下来并归还任何有用的东西。最终的结果是,我想添加时间模块,每X分钟运行一次,然后让它在每个站点变红时在某处记录一条消息。(这一切都是为了让我妻子能够找出新泽西州最不拥挤的车管所!)
非常感谢任何帮助或指导,使BeautifulSoup钻头正常工作
该表实际上是从this站点加载的
要仅获取红色文本,您可以使用CSS选择器
soup.select('font[color="red"]')
,正如@Mr.Polywhill所提到的:数据从其他位置加载,在本例中为
'https://www.state.nj.us/mvc/locations/agency.htm'
。要获取每个城镇的城镇+标题,可以使用以下示例:印刷品:
相关问题 更多 >
编程相关推荐