使用Python,我希望从reddit.com获得完整的HTML代码来搜索字符串,但是我只能得到一个奇怪的小版本。下面的if语句中的代码没有运行,但应该运行,因为我知道整个页面源上都存在字符串(在browser dev tools和“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()有效的方法是将变量内容写入文件:
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()我需要能够在Python中搜索这个HTML,而不必创建一个文件。
我尝试了以下几点,但都没有成功:
driver.page_source返回的字符串,将其粘贴到记事本中并保存为.html文件。sourceHtml变量。getDOM = driver.execute_script('return document.documentElement.outerHTML')time.sleep(5)等待页面在运行driver.page_source之前完成加载(尽管WebDriver无论如何在回调之前都要等待完整的响应)。在此之前,非常感谢您。
发布于 2020-03-21 14:16:38
我发现了问题。
我的if语句中的字符串也意味着'User帐户‘而不是'user帐户’,所以它是在计算false。
当我通过调试模式将变量值保存为html文件时,它只是变量实际保存量的一小部分。这些调试变量值的限制因开发人员环境(Netbeans、Eclipse、VS Code等)而异。在VS代码中,它似乎是65,536字节(2^16)。
我没有讨论下一步,但是如果您确实想要更改调试变量大小限制,我认为在launch.json文件中添加了一个“key:value”(取决于您正在调试的语言)。这是如何为PHP做这件事。
当您“启动”到调试模式时,launch.json文件将应用设置。该文件中有一个名为“.”的蓝色按钮。它将列出可以应用于该文件的所有设置。
https://stackoverflow.com/questions/60781973
复制相似问题