【问题标题】:how to copy all the code of a URL with python如何使用python复制URL的所有代码
【发布时间】:2017-12-06 18:27:42
【问题描述】:

我想用 Python 3.6 复制一个 URL (http://modelseed.org/biochem/reactions/rxn00001) 的所有代码,但是我只能复制一部分代码,我不知道为什么。

到目前为止,我尝试使用“请求”模块

import requests
page = requests.get("http://modelseed.org/biochem/reactions/rxn00001")
print(page.content)

和“urllib”

import urllib.request
site = urllib.request.urlopen("http://modelseed.org/biochem/reactions/rxn00001")
print(site.read())

缺少“反应详细信息”信息的代码部分,例如“名称”、“ID”和“缩写”,但如果我在 Chrome 的开发人员栏上检查代码,它们是可见的。

我可以使用上面的两个代码下载的代码是:

<!DOCTYPE html>
<html lang="en" ng-app="ModelSEED">
 <head>
  <base href="/"/>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
  <meta content="IE=edge" http-equiv="X-UA-Compatible"/>
  <meta content="initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
   <meta content="The ModelSEED is a resource for the reconstruction, exploration, comparison, and analysis of metabolic models." name="description"/>
   <link href="/img/ModelSEED-favicon.png?v=2.0" rel="shortcut icon"/>
   <meta content="nconrad" name="author"/>
   <title>
    ModelSEED
   </title>
   <link href="components/angular-material/angular-material.css" rel="stylesheet"/>
   <link href="components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
   <!-- to be removed -->
   <link href="components/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
   <link href="icomoon/style.css" rel="stylesheet"/>
   <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
   <link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css"/>
   <link href="build/style.css" rel="stylesheet"/>
   <!--<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>-->
   <script src="build/site.js">
   </script>
   <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
   <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
   <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </meta>
 </head>
 <body>
  <div style="height: 100%;" ui-view="">
  </div>
  <script>
   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-67412611-1', 'auto');
      ga('send', 'pageview');
  </script>
 </body>
</html>

任何人都知道为什么 和(

之后

【问题讨论】:

    标签: python html python-3.x python-requests urllib


    【解决方案1】:

    它是由 javascript 脚本插入的,因此,无论是 requests 还是 urllib 都可以找到它,您需要使用浏览器,您应该尝试使用 selenium 或 PhantomJS

    类似:

    from selenium import webdriver
    
    driver = webdriver.Chrome('./chromedriver')
    driver.get(url)
    driver.page_source
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 这并不完全是我问题的答案,但它确实让我的生活变得轻松了很多。谢谢。
      猜你喜欢
      • 2012-07-19
      • 2021-04-28
      • 2019-12-01
      • 2019-04-26
      • 2016-10-04
      • 1970-01-01
      • 2018-08-02
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多