动态内容的Python抓取(视觉上与html源代码不同)
我是stackoverflow的超级粉丝,通常通过这个网站找到解决问题的方法。然而,以下问题困扰了我太久,迫使我在这里创建一个帐户并直接询问: 我试图替换这个链接:我想要的是“TRCS资产类别”和“货币””这两行 首先,我使用以下代码:动态内容的Python抓取(视觉上与html源代码不同),python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我是stackoverflow的超级粉丝,通常通过这个网站找到解决问题的方法。然而,以下问题困扰了我太久,迫使我在这里创建一个帐户并直接询问: 我试图替换这个链接:我想要的是“TRCS资产类别”和“货币””这两行 首先,我使用以下代码: from bs4 import BeautifulSoup import urllib2 url = 'https://permid.org/1-21475776041' req = urllib2.urlopen(url) raw = req.read()
from bs4 import BeautifulSoup
import urllib2
url = 'https://permid.org/1-21475776041'
req = urllib2.urlopen(url)
raw = req.read()
soup = BeautifulSoup(raw)
print soup.prettify()
返回的html代码(见下文)与单击链接时在浏览器中看到的不同:
<!DOCTYPE html>
<!--[if lt IE 7]> <html ng-app="tmsMdaasApp" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html ng-app="tmsMdaasApp" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html ng-app="tmsMdaasApp" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" ng-app="tmsMdaasApp">
<!--<![endif]-->
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
<meta charset="utf-8"/>
<meta content="ie=edge" http-equiv="x-ua-compatible"/>
<meta content="max-age=0,no-cache" http-equiv="Cache-Control"/>
<base href="/"/>
<title ng-bind="PageTitle">
Thomson Reuters | PermID
</title>
<meta content="" name="description"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<meta content="#ff8000" name="theme-color"/>
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<link href="app/vendor.daf96efe.css" rel="stylesheet"/>
<link href="app/app.1405210f.css" rel="stylesheet"/>
<link href="favicon.ico" rel="icon"/>
<!-- Typekit -->
<script src="//use.typekit.net/gnw2rmh.js">
</script>
<script>
try{Typekit.load({async:true});}catch(e){}
</script>
<!-- // Typekit -->
<!-- Google Tag Manager Data Layer -->
<!--<script>
analyticsEvent = function() {};
analyticsSocial = function() {};
analyticsForm = function() {};
dataLayer = [];
</script>-->
<!-- // Google Tag Manager Data Layer -->
</head>
<body class="theme-grey" id="top" ng-esc="">
<!--[if lt IE 7]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<navbar class="tms-navbar">
</navbar>
<div id="body" role="main" ui-view="">
</div>
<div id="footer-wrapper" ng-show="!params.elementsToHide">
<footer id="main-footer">
</footer>
</div>
<!--[if lt IE 9]>
<script src="bower_components/es5-shim/es5-shim.js"></script>
<script src="bower_components/json3/lib/json3.min.js"></script>
<![endif]-->
<script src="app/vendor.8cc12370.js">
</script>
<script src="app/app.6e5f6ce8.js">
</script>
</body>
</html>
汤森路透|许可
尝试{Typekit.load({async:true});}catch(e){}
有人知道我在这里遗漏了什么,以及我如何让它工作吗?在大量页面中使用默认用户代理会给您一个不同的页面,因为它使用的是过时的用户代理。这是您的输出告诉您的
虽然这可能是您的问题,但它并不能完全回答在网页上动态应用更改的问题。要获取动态更改的数据,需要模拟页面加载时发出的javascript请求。如果您发出javascript正在发出的请求,您将获得javascript正在获得的数据。使用具有大量页面的默认用户代理将为您提供一个外观不同的页面,因为它使用的是过时的用户代理。这是您的输出告诉您的
虽然这可能是您的问题,但它并不能完全回答在网页上动态应用更改的问题。要获取动态更改的数据,需要模拟页面加载时发出的javascript请求。如果您发出javascript发出的请求,您将获得javascript获得的数据。谢谢,Teemu Risikko-您链接的网站的评论(尽管不是解决方案)让我走上了正确的道路 如果其他人遇到同样的问题,我的解决方案是:我通过请求获取数据,而不是通过传统的“刮取”(如BeautifulSoup或lxml)
{u'Asset Class': [u'Units'],
u'Asset Class URL': [u'https://permid.org/1-302043'],
u'Currency': [u'CAD'],
u'Currency URL': [u'https://permid.org/1-500140'],
u'Exchange': [u'TOR'],
u'IsQuoteOf.mdaas': [{u'Is Quote Of': [u'Convertible Debentures Income Units'],
u'URL': [u'https://permid.org/1-21475768667'],
u'quoteOfInstrument': [u'21475768667'],
u'quoteOfInstrument URL': [u'https://permid.org/1-21475768667']}],
u'Mic': [u'XTSE'],
u'PERM ID': [u'21475776041'],
u'Quote Name': [u'CONVERTIBLE DEBENTURES INCOME UNT'],
u'Quote Type': [u'equity'],
u'RIC': [u'OCV_u.TO'],
u'Ticker': [u'OCV.UN'],
u'entityType': [u'Quote']}
谢谢,Teemu Risikko-你链接的网站的评论(尽管不是解决方案)让我走上了正确的道路 如果其他人遇到同样的问题,我的解决方案是:我通过请求获取数据,而不是通过传统的“刮取”(如BeautifulSoup或lxml)
{u'Asset Class': [u'Units'],
u'Asset Class URL': [u'https://permid.org/1-302043'],
u'Currency': [u'CAD'],
u'Currency URL': [u'https://permid.org/1-500140'],
u'Exchange': [u'TOR'],
u'IsQuoteOf.mdaas': [{u'Is Quote Of': [u'Convertible Debentures Income Units'],
u'URL': [u'https://permid.org/1-21475768667'],
u'quoteOfInstrument': [u'21475768667'],
u'quoteOfInstrument URL': [u'https://permid.org/1-21475768667']}],
u'Mic': [u'XTSE'],
u'PERM ID': [u'21475776041'],
u'Quote Name': [u'CONVERTIBLE DEBENTURES INCOME UNT'],
u'Quote Type': [u'equity'],
u'RIC': [u'OCV_u.TO'],
u'Ticker': [u'OCV.UN'],
u'entityType': [u'Quote']}
要渲染动态内容,需要使用浏览器。请查看Selenium以执行此操作。如果要呈现需要使用浏览器的动态内容,可能需要复制。请查看Selenium以执行此操作。可能与