Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用selenium通过javascript呈现源代码获取html_Javascript_Python_Selenium - Fatal编程技术网

如何使用selenium通过javascript呈现源代码获取html

如何使用selenium通过javascript呈现源代码获取html,javascript,python,selenium,Javascript,Python,Selenium,我在一个网页中运行一个查询,然后得到结果url。如果我右键单击“查看html源代码”,我可以看到由JS生成的html代码。如果我只是使用urllib,python就无法获得JS代码。所以我看到了一些使用硒的解决方案。这是我的密码: from selenium import webdriver url = 'http://www.archives.com/member/Default.aspx?_act=VitalSearchResult&lastName=Smith&state=

我在一个网页中运行一个查询,然后得到结果url。如果我右键单击“查看html源代码”,我可以看到由JS生成的html代码。如果我只是使用urllib,python就无法获得JS代码。所以我看到了一些使用硒的解决方案。这是我的密码:

from selenium import webdriver
url = 'http://www.archives.com/member/Default.aspx?_act=VitalSearchResult&lastName=Smith&state=UT&country=US&deathYear=2004&deathYearSpan=10&location=UT&activityID=9b79d578-b2a7-4665-9021-b104999cf031&RecordType=2'
driver = webdriver.PhantomJS(executable_path='C:\python27\scripts\phantomjs.exe')
driver.get(url)
print driver.page_source

>>> <html><head></head><body></body></html>         Obviously It's not right!!
从selenium导入webdriver
url='1〕http://www.archives.com/member/Default.aspx?_act=VitalSearchResult&lastName=Smith&state=UT&country=US&deathYear=2004&deathYearSpan=10&location=UT&activityID=9b79d578-b2a7-4665-9021-b104999cf031&RecordType=2'
driver=webdriver.PhantomJS(可执行文件_path='C:\python27\scripts\PhantomJS.exe')
获取驱动程序(url)
打印driver.page\u源
>>>显然这是不对的!!
这是我在右键单击窗口中需要的源代码(我想要信息部分)


您需要通过
javascript
获取文档,您可以使用seleniums
执行脚本
功能

from time import sleep # this should go at the top of the file

sleep(5)
html = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")
print html

这将获得
标记中的所有内容

我认为您是在JavaScript呈现动态HTML之前获得了源代码

最初尝试在导航和获取页面源代码之间放置几秒钟的睡眠时间


如果可行,则可以更改为不同的等待策略。

无需使用该解决方案,您可以使用:

driver = webdriver.PhantomJS()
driver.get('http://www.google.com/')
html = driver.find_element_by_tag_name('html').get_attribute('innerHTML')

我遇到了同样的问题,并最终通过所需的功能解决了问题

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.common.proxy import ProxyType

proxy = Proxy(
     {
          'proxyType': ProxyType.MANUAL,
          'httpProxy': 'ip_or_host:port'
     }
)
desired_capabilities = webdriver.DesiredCapabilities.PHANTOMJS.copy()
proxy.add_to_capabilities(desired_capabilities)
driver = webdriver.PhantomJS(desired_capabilities=desired_capabilities)
driver.get('test_url')
print driver.page_source

你试试看
drysrave
这个浏览器是完全支持重js代码的试试看我希望它对你有用我在从互联网上获取Javascript源代码方面也有同样的问题,我用Victory的建议解决了这个问题

*首先
执行脚本

driver=webdriver.Chrome()
driver.get(urls)
innerHTML = driver.execute_script("return document.body.innerHTML")
#print(driver.page_source)
*Second:使用
beautifulsop
解析html(您可以通过pip命令下载
beautifulsop

*Third:打印出所需的值

 for span in viewcount:
    print(span.string) 
*完整代码

from selenium import webdriver
import lxml

urls="http://www.archives.com/member/Default.aspx?_act=VitalSearchResult&lastName=Smith&state=UT&country=US&deathYear=2004&deathYearSpan=10&location=UT&activityID=9b79d578-b2a7-4665-9021-b104999cf031&RecordType=2"

driver = webdriver.PhantomJS()


##driver=webdriver.Chrome()
driver.get(urls)
innerHTML = driver.execute_script("return document.body.innerHTML")
##print(driver.page_source)

import bs4
import re
from time import sleep

sleep(1)
root=bs4.BeautifulSoup(innerHTML,"lxml")
viewcount=root.find_all("span",attrs={'class':'short-view-count style-scope yt-view-count-renderer'})


for span in viewcount:
print(span.string)

driver.quit()

您想要的html代码在页面上是什么样子的?您可能希望使用selenium的
get\u element\u by.*
函数之一,但具体如何使用取决于html本身。我指的是所有功能。例如,您在google中输入了一些内容。在结果网页中,右键单击,查看源代码。这就是我想要的“一切”。它看起来很有效,但只给了我,我在那里重新定义了我的问题,你能再看一下这个问题吗?多谢各位much@MacSanhe查看我的编辑,如果页面未完全加载,您将无法获取所有正文内容。还可以尝试转到页面并在调试器控制台中运行document.getElementsByTagName('html')[0]。innerHTML
,查看DOM的使用量。有人知道是否有一种方法可以在不使用Selenium之类的浏览器的情况下获取页面的javascript吗?@Wilson-您应该创建一个顶级问题。在评论中很难澄清和回答您的问题。请尝试使用此参数加载PhantomJS。browser=webdriver.PhantomJS(service_args=['--ignore ssl errors=true'])它对methis有效这是一个注释,不是答案这是一个陈旧的、稍微过时的答案,但它让我想到了用mitmproxy捕捉javascript,所以+1但是如果你需要单击元素或输入文本怎么办?@AntoninGAVREL如果睡眠对你有效,您现在可能希望实施更稳定的等待策略;e、 g.使用“sleep”来显示某个特定元素是一种固有的不稳定状态。谢谢Robbi,这确实是我所做的:
myclass='question'
wait.until(元素的可见性位于((By.CLASS\u NAME,myclass)))
 for span in viewcount:
    print(span.string) 
from selenium import webdriver
import lxml

urls="http://www.archives.com/member/Default.aspx?_act=VitalSearchResult&lastName=Smith&state=UT&country=US&deathYear=2004&deathYearSpan=10&location=UT&activityID=9b79d578-b2a7-4665-9021-b104999cf031&RecordType=2"

driver = webdriver.PhantomJS()


##driver=webdriver.Chrome()
driver.get(urls)
innerHTML = driver.execute_script("return document.body.innerHTML")
##print(driver.page_source)

import bs4
import re
from time import sleep

sleep(1)
root=bs4.BeautifulSoup(innerHTML,"lxml")
viewcount=root.find_all("span",attrs={'class':'short-view-count style-scope yt-view-count-renderer'})


for span in viewcount:
print(span.string)

driver.quit()