gpt4 book ai didi

python-3.x - 使用 Python 从网站请求中获取完整的 html

转载 作者:行者123 更新时间:2023-12-04 14:20:42 24 4
gpt4 key购买 nike

我正在尝试向网站(例如 Digikey)发送 http 请求并读回完整的 html。例如,我正在使用此链接:https://www.digikey.com/products/en?keywords=part_number获取部件号,例如:https://www.digikey.com/products/en?keywords=511-8002-KIT .但是我得到的不是完整的 html。

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.digikey.com/products/en?keywords=511-8002-KIT')
soup = BeautifulSoup(r.text)
print(soup.prettify())

输出:

<!DOCTYPE html>
<html>
<head>
<script>
var i10cdone =(function(){ function pingBeacon(msg){ var i10cimg = document.createElement('script'); i10cimg.src='/i10c@p1/botox/file/nv-loaded.js?status='+window.encodeURIComponent(msg); i10cimg.onload = function(){ (document.head || document.documentElement).removeChild(i10cimg) }; i10cimg.onerror = function(){ (document.head || document.documentElement).removeChild(i10cimg) }; ( document.head || document.documentElement).appendChild(i10cimg) }; pingBeacon('loaded'); if(String(document.cookie).indexOf('i10c.bdddb=c2-f0103ZLNqAeI3BH6yYOfG7TZlRtCrMwqUo')>=0) { document.cookie = 'i10c.bdddb=;path=/';}; var error=''; function errorHandler(e) { if (e && e.error && e.error.stack ) { error=e.error.stack; } else if( e && e.message ) { error = e.message; } else { error = 'unknown';}} if(window.addEventListener) { window.addEventListener('error',errorHandler, false); } else { if ( window.attachEvent ){ window.attachEvent('onerror',errorHandler); }} return function(){ if (window.removeEventListener) {window.removeEventListener('error',errorHandler); } else { if (window.detachEvent) { window.detachEvent('onerror',errorHandler); }} if(error) { pingBeacon('error-' + String(error).substring(0,500)); document.cookie='i10c.bdddb=c2-f0103ZLNqAeI3BH6yYOfG7TZlRtCrMwqUo;path=/'; }}; })();
</script>
<script src="/i10c@p1/client/latest/auto/instart.js?i10c.nv.bucket=pci&amp;i10c.nv.host=www.digikey.com&amp;i10c.opts=botox&amp;bcb=1" type="text/javascript">
</script>
<script type="text/javascript">
INSTART.Init({"apiDomain":"assets.insnw.net","correlation_id":"1553546232:4907a9bdc85fe4e8","custName":"digikey","devJsExtraFlags":"{\"disableQuerySelectorInterception\" :true, 'rumDataConfigKey':'/instartlogic/clientdatacollector/getconfig/monitorprod.json','custName':'digikey','propName':'northamerica'}","disableInjectionXhr":true,"disableInjectionXhrQueryParam":"instart_disable_injection","iframeCommunicationTimeout":3000,"nanovisorGlobalNameSpace":"I10C","partialImage":false,"propName":"northamerica","rId":"0","release":"latest","rum":false,"serveNanovisorSameDomain":true,"third_party":["IA://www.digikey.com/js/geotargeting.js"],"useIframeRpc":false,"useWrapper":false,"ver":"auto","virtualDomains":4,"virtualizeDomains":["^auth\\.digikey\\.com$","^authtest\\.digikey\\.com$","^blocked\\.digikey\\.com$","^dynatrace\\.digikey\\.com$","^search\\.digikey\\.com$","^www\\.digikey\\.ca$","^www\\.digikey\\.com$","^www\\.digikey\\.com\\.mx$"]}
);
</script>
<script>
typeof i10cdone === 'function' && i10cdone();
</script>
</head>
<body>
<script>
setTimeout(function(){document.cookie="i10c.eac23=1";window.location.reload(true);},30);
</script>
</body>
</html>

我需要完整 html 的原因是要在其中搜索特定关键字,例如特定零件编号结果中是否出现术语“无铅”或“通孔”。我不仅为 Digikey 这样做,也为其他网站这样做。

如有任何帮助,我们将不胜感激!

谢谢!

编辑:

感谢大家的建议/回答。此处为对此感兴趣的其他人提供更多信息:Web-scraping JavaScript page with Python

最佳答案

您要查找的页面部分很可能包含使用 Javascript 动态生成的内容。

在浏览器上访问 view-source:https://www.digikey.com/products/en?keywords=part_number,您会看到请求正在获取完整的 html - 它只是没有执行Javascript 代码。

如果您右键单击并单击检查(Chrome),您将看到执行 javascript 代码后创建的最终 DOM。

要获取呈现的内容,您需要使用完整的网络驱动程序,例如 Selenium能够执行 Javascript 以呈现整个页面。

这里是一个如何使用 Selenium 实现该目标的示例:

How can I parse a website using Selenium and Beautifulsoup in python?

In [8]: from bs4 import BeautifulSoup
In [9]: from selenium import webdriver
In [10]: driver = webdriver.Firefox()
In [11]: driver.get('http://news.ycombinator.com')
In [12]: html = driver.page_source
In [13]: soup = BeautifulSoup(html)
In [14]: for tag in soup.find_all('title'):
....: print tag.text
....:
....:
Hacker News

关于python-3.x - 使用 Python 从网站请求中获取完整的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55346142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com