Selenium - 为什么 driver.page_source 的值只有在写入文件时才能正确解析?
Selenium - Why is the value of driver.page_source only parsing correctly when written to a file?
使用 Python,我想从 reddit.com 获取完整的 HTML 代码来搜索字符串,但是我只能得到它的一个奇怪的小版本.下面的 if 语句中的代码 没有 运行 但它应该是因为我知道该字符串存在于整页源代码中(在浏览器开发工具和 'View page Source' 浏览器功能):
driver = webdriver.Firefox()
driver.get('https://www.reddit.com')
driver.add_cookie({'name':'reddit_session', 'value':'###session cookie value goes here###', 'path':'/', 'domain':'reddit.com'})
driver.refresh() # refresh the page to apply the cookie
source_html = driver.page_source
if 'user account' in source_html:
print("String found.")
driver.close()
Here is sourceHTML
copy and pasted into a file. It is 65,536 bytes long and doesn't make sense.
有效的是将变量内容写入文件:
driver = webdriver.Firefox()
driver.get('https://www.reddit.com')
driver.add_cookie({'name':'reddit_session', 'value':'###session cookie value goes here###', 'path':'/', 'domain':'reddit.com'})
driver.refresh() # refresh the page to apply the cookie
source_html = driver.page_source
with open('page.html', 'w') as myfile:
myfile.write(source_html)
driver.close()
And here is the 580,000 bytes of HTML that I am expecting that was written to the file.
我需要能够在 Python 中搜索此 HTML 而不必创建文件。
我尝试了以下但没有成功:
- 调试时,复制从
driver.page_source
返回的字符串,将其粘贴到记事本中并另存为 .html 文件。
- 使用 BeautifulSoup 解析
sourceHtml
变量。
- 执行Javascript得到整个DOM:
getDOM = driver.execute_script('return document.documentElement.outerHTML')
- 使用
time.sleep(5)
等待页面在 运行ning driver.page_source
之前完成加载(尽管 Webdriver 在回调之前无论如何都会等待完整的响应)。
非常感谢。
我发现了问题。
Copying a variable's value in debug mode does not give you the full value.
我的 if
语句中的字符串也应该是 'User account' 而不是 'user account ' 所以它正在评估 false
.
当我通过调试模式将变量值保存为 html 文件时,它只是变量实际保存值的一小部分。这些调试变量值的限制因开发人员环境(Netbeans、Eclipse、VS Code 等)而异。在 VS Code 中,它似乎是 65,536 字节 (2^16)。
我单独留下了下一步,但如果您确实想更改调试变量大小限制,我想您可以将 'key:value' 添加到 launch.json
文件(取决于什么您正在调试的语言)。 This is how to do it for PHP。
launch.json
文件在您 'launch' 进入调试模式时应用设置。该文件中有一个名为 'Add configuration...' 的蓝色按钮。它将列出您可以应用于该文件的所有设置。
使用 Python,我想从 reddit.com 获取完整的 HTML 代码来搜索字符串,但是我只能得到它的一个奇怪的小版本.下面的 if 语句中的代码 没有 运行 但它应该是因为我知道该字符串存在于整页源代码中(在浏览器开发工具和 'View page Source' 浏览器功能):
driver = webdriver.Firefox()
driver.get('https://www.reddit.com')
driver.add_cookie({'name':'reddit_session', 'value':'###session cookie value goes here###', 'path':'/', 'domain':'reddit.com'})
driver.refresh() # refresh the page to apply the cookie
source_html = driver.page_source
if 'user account' in source_html:
print("String found.")
driver.close()
Here is sourceHTML
copy and pasted into a file. It is 65,536 bytes long and doesn't make sense.
有效的是将变量内容写入文件:
driver = webdriver.Firefox()
driver.get('https://www.reddit.com')
driver.add_cookie({'name':'reddit_session', 'value':'###session cookie value goes here###', 'path':'/', 'domain':'reddit.com'})
driver.refresh() # refresh the page to apply the cookie
source_html = driver.page_source
with open('page.html', 'w') as myfile:
myfile.write(source_html)
driver.close()
And here is the 580,000 bytes of HTML that I am expecting that was written to the file.
我需要能够在 Python 中搜索此 HTML 而不必创建文件。
我尝试了以下但没有成功:
- 调试时,复制从
driver.page_source
返回的字符串,将其粘贴到记事本中并另存为 .html 文件。 - 使用 BeautifulSoup 解析
sourceHtml
变量。 - 执行Javascript得到整个DOM:
getDOM = driver.execute_script('return document.documentElement.outerHTML')
- 使用
time.sleep(5)
等待页面在 运行ningdriver.page_source
之前完成加载(尽管 Webdriver 在回调之前无论如何都会等待完整的响应)。
非常感谢。
我发现了问题。
Copying a variable's value in debug mode does not give you the full value.
我的 if
语句中的字符串也应该是 'User account' 而不是 'user account ' 所以它正在评估 false
.
当我通过调试模式将变量值保存为 html 文件时,它只是变量实际保存值的一小部分。这些调试变量值的限制因开发人员环境(Netbeans、Eclipse、VS Code 等)而异。在 VS Code 中,它似乎是 65,536 字节 (2^16)。
我单独留下了下一步,但如果您确实想更改调试变量大小限制,我想您可以将 'key:value' 添加到 launch.json
文件(取决于什么您正在调试的语言)。 This is how to do it for PHP。
launch.json
文件在您 'launch' 进入调试模式时应用设置。该文件中有一个名为 'Add configuration...' 的蓝色按钮。它将列出您可以应用于该文件的所有设置。