当我使用driver.page_source时,我将获得完整的源代码页面,有什么方法可以获取html代码的特定部分。
from selenium import webdriver
chrome_options = webdriver.ChromeOptions ()
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome (executable_path="/selenium/chromedriver", options=chrome_options)
driver.get("https://news.creaders.net/us/2021/01/27/2315313.html")
content = driver.page_source
然后,我将收到整页的html。
但是我只需要在里面的html: <div id="newsContent"> </div>
<div id="newsContent">
<p></p><p>cotent</p><p style="text-align: center;"><img src="https://pub.creaders.net/upload_files/image/202101/20210127_16117914118079.png" title="20210127_16117914118079.png" alt="image.png"></p>
</div>
尝试通过BeautifulSoup解析器运行HTML输出。
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
div = soup.find('div', id='newsContent')
print ''.join(map(str, div.contents))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句