我有一个HTML字符串,该字符串从PhanthomJs在浏览器中打开,并尝试保存结果URL。
我的代码如下所示。
driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))
element = driver.find_element_by_tag_name('body')
elem = element.find_element_by_tag_name('noscript')
print elem.find_element_by_tag_name('img')
html_content
如下所示。
<script language="javascript" src="https://somejs"></script>
<noscript>
<a href="https://track.adform.net/C/?bn=15864640;C=0" target="_blank">
<img src="https://actualimage.net/verbserve/?bn=155679864640;srctype=4;ord=[timestamp]" border="0" width="728" height="90" alt=""/>
</a>
</noscript>
当我在本地以html文件格式运行图像时,在html上方呈现图像。
我想用上面提到的代码保存该图像。但是很遗憾,我无法使用find_element_by_tag_name
我遇到了错误。
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with tag name 'img'","request"
请让我知道我在这里可能做错了什么。
答案 0 :(得分:1)
如果简单的HTML内容不是网页内容,请尝试
from bs4 import BeautifulSoup
html_content = """ <script language="javascript" src="https://somejs"></script>
<noscript>
<a href="https://track.adform.net/C/?bn=15864640;C=0" target="_blank">
<img src="https://actualimage.net/verbserve/?bn=155679864640;srctype=4;ord=[timestamp]" border="0" width="728" height="90" alt=""/>
</a>
</noscript>"""
sp = BeautifulSoup(html_content,'html.parser')
elem = sp.find('noscript')
img = elem.find('img')
print(img['src'])
O / P:
https://actualimage.net/verbserve/?bn=155679864640;srctype=4;ord=[timestamp]
按网站URL截取网页内容:
driver = webdriver.Chrome("/usr/bin/chromedriver")
driver.get('http://www.test.com')
sp = BeautifulSoup(driver.page_source,'html.parser')
elem = sp.find('noscript')
img = elem.find('img')
print(img['src'])
"/usr/bin/chromedriver"
Chrome浏览器驱动器的路径