问题标签 [bs4]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
53 浏览

python - 读取和附加文件上下文管理器:似乎不读取,只写入

我正在尝试读取并附加到文件,但是当我使用上下文管理器时,它似乎不起作用。

在此代码中,我试图获取包含我的“序列”列表中的项目之一的站点上的所有链接。如果链接在列表中,我首先检查链接是否已经在文件中。如果找到链接,则不应再次附加该链接。但确实如此。

我要么猜测我没有使用正确的模式,要么我以某种方式搞砸了我的上下文管理器。还是我完全错了

这是我第一次使用上下文管理器。提示将不胜感激。

编辑:没有上下文管理器的类似项目。在这里,我也尝试使用上下文管理器,但在遇到同样的问题后放弃了。

并使用上下文管理器:

0 投票
1 回答
1606 浏览

python - Python 和 BeautifulSoup4 - 从 TD 标签中提取文本

在查看了许多其他问题后,我被困住了。我的代码目前正在将数据分成命名行,但返回整行而不是仅包含文本,我只是从以下行中寻找 ASCO VALVE MFG., INC.:我不确定如何取出该行中的文本。

我的输入看起来像:标题:

数据行如下:

我的代码目前正在将数据分解为命名行,但正在返回整个 html 行。

我最终想要的是一个字典,我猜想[CSNO 中的文本] 和 [SN 中的文本] 对与第二个 CSV 文件匹配。我希望一切都说得通。

0 投票
1 回答
57 浏览

python - BeautifulSoup4 无法正确打印。Python3

我目前正处于 Python3 的学习过程中,我正在为一些数据抓取一个站点,效果很好,但是在打印出 p 标签时,我无法按预期工作。

一切正常,但打印是“字节”

ascii 编码的真实示例文本:

请注意,Announcement 是 p,其余是 'strong' 在 ap 标签下。

使用 utf-8 编码的相同样本

我希望得到:

如您所见,不正确的字符在“ascii”中被删除,但有些字符会 破坏一些换行符,我还没有弄清楚如何正确打印,而且 b 仍然在那里!

我真的不知道如何删除 b 并正确编码或解码。我已经尝试了所有可以搜索到的“解决方案”。

HTML 内容 = utf-8

我最不想在处理之前更改完整的数据,因为它会扰乱我的其他工作,我认为没有必要。

美化不起作用。

有什么建议么?

0 投票
2 回答
303 浏览

javascript - 如何按顺序获取所有元素文本

我正在使用 bs4 从表格中获取文本

但是我怎样才能有 2 个 findall 顺序呢?我在下面试过

headings = [i.get_text() for i in table.find("tr").find_all("td").find_all("div")]

如果我只是使用find("td"),我只会得到一个值,而不是表中的所有值。我如何循环遍历"td"每个元素"div"

0 投票
0 回答
260 浏览

python - 如何使用 python 和 BeautifulSoup4 从表行中获取特定的表数据

我到处研究过,但遇到了麻烦......我想访问每个单独的元素并将值附加到我的空 python 列表中......当然,在我的 python 列表中有正确的数据之后,我要去将它们附加到 csv 文件...

0 投票
0 回答
58 浏览

python - BS4 中的迭代在网络抓取中失败

我正在使用漂亮的汤 4 从完整的赛狗会议(英国)中抓取数据。这是一个 Url 的示例。 http://www.gbgb.org.uk/resultsMeeting.aspx?id=135549 每次会议通常有 9 到 14 场比赛。下面的代码遍历卡片上的每个比赛(事件)并将数据打印到屏幕上(PyCharm Python v3)。问题是 BS 没有完成迭代并且通常会失败大约在卡片上的第 7 或第 8 场比赛(赛事)左右,在某些情况下,通过仅获取一半跑步者的数据,在赛事(比赛)中途中断。在某些情况下,我收到标准消息“过程已完成退出代码 0“我确实认为这可能与 Url 暂时不可用有关,但程序似乎总是默认在第 7 或 8 场比赛事件左右是不寻常的。我已经搜索了各个页面的源代码和可以看到代码中没有不一致(承认我对 HTML 不太熟悉)任何建议表示赞赏。

0 投票
1 回答
243 浏览

python-2.7 - 使用匹配的 TD BS4 获取 TR 的最后一个 TD

我怎样才能抓住其中有“年终”的任何行的最后一个 TD?本质上是想抓住“魔法!” 以上。

我可以很好地搜索“年终”,只是不确定如何迭代到“年终”TD所在的TR的最后一个TD。

0 投票
4 回答
4846 浏览

python - Python: print/get first sentence of each paragraph

This is the code I have, but it prints the whole paragraph. How to print the first sentence only, up to the first dot?

This code prints:

To state that the human brain has capabilities that are, in some respects, far superior to those of all other known objects in the cosmos would be uncontroversial. The brain is the only kind of object capable of understanding that the cosmos is even there, or why there are infinitely many prime numbers, or that apples fall because of the curvature of space-time, or that obeying its own inborn instincts can be morally wrong, or that it itself exists. Nor are its unique abilities confined to such cerebral matters. The cold, physical fact is that it is the only kind of object that can propel itself into space and back without harm, or predict and prevent a meteor strike on itself, or cool objects to a billionth of a degree above absolute zero, or detect others of its kind across galactic distances.

BUT I ONLY want it to print:

To state that the human brain has capabilities that are, in some respects, far superior to those of all other known objects in the cosmos would be uncontroversial.

Thanks for help

0 投票
1 回答
3878 浏览

python - 如何从内部提取链接

:美汤

我正在尝试提取一个这样写的链接:

<h2 class="section-heading">
    <a href="http://www.nytimes.com/pages/arts/index.html">Arts »</a>
</h2>

我的代码是:

0 投票
1 回答
78 浏览

python - BS4 从名字奇怪的班级获取信息

从Steam 社区市场搜索中获得了这个奇怪的 html :

如何用bs4提取数据?这不起作用: