擅长:python、mysql、java
<p>是的,这是因为一些javascript生成的内容是在页面加载时由浏览器呈现的,但是在javascript进行各种操作(添加内容)之后,您看到的是最终的DOM。要解决这个问题,您需要使用<code>selenium</code>,但是它有很多缺点,比如速度和内存问题。在我看来,一种更现代、更好的方法是使用<a href="https://pypi.org/project/requests-html/" rel="nofollow noreferrer">requests-html</a>,它将同时替换<code>bs4</code>和<code>urllib</code>,并且它有文档中提到的<code>render</code>方法。你知道吗</p>
<p>下面是一个使用<code>requests_html</code>的示例代码,请记住您试图打印的不是<code>utf8</code>,因此在某些编辑器(如sublime)上打印时可能会遇到一些问题,使用cmd运行良好。你知道吗</p>
<pre><code>from requests_html import HTMLSession
session = HTMLSession()
r = session.get("https://translate.google.com/#view=home&op=translate&sl=en&tl=en&text=hello")
r.html.render()
css = ".source-input .tlid-transliteration-content"
print(r.html.find(css, first=True).text)
# output: heˈlō,həˈlō
</code></pre>