擅长:python、mysql、java
<p>我稍微修改了一下你的HTML。(请注意,反斜杠应该是斜杠。)</p>
<p>为此,首先找到“Hello”字符串。在for循环中调用这些字符串之一<code>s</code>。那么你想要的是<code>s.findParent().findNextSibling()</code>。在</p>
<p>我显示<code>s</code>、<code>s.findParent()</code>和{<cd2>}来展示我如何从这些字符串中构造出您需要的东西。在</p>
<pre><code>>>> import bs4
>>> HTML = '''\
... <i>Hello</i>
... <a href="www.google.com"> Google </a>
... <i>Bye</i>
... <a href="www.google.com"> Google2 </a>
... <i>Hello</i>
... <a href="www.google.com"> Google3 </a>
... <i>Bye</i>
... '''
>>> soup = bs4.BeautifulSoup(HTML, 'lxml')
>>> for s in soup.find_all(string='Hello'):
... s, s.findParent(), s.findParent().findNextSibling()
...
('Hello', <i>Hello</i>, <a href="www.google.com"> Google </a>)
('Hello', <i>Hello</i>, <a href="www.google.com"> Google3 </a>)
</code></pre>