Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过python-3.6在网站html中搜索?_Python_Html_Search_Python Requests_Python 3.6 - Fatal编程技术网

如何通过python-3.6在网站html中搜索?

如何通过python-3.6在网站html中搜索?,python,html,search,python-requests,python-3.6,Python,Html,Search,Python Requests,Python 3.6,我有很多礼物,我需要创建检查器,这将检查礼物是否有效-->它将在html中搜索一些单词。我在寻找“礼物代码无效” 当我试图通过urllib或请求读取html时,它只会加载html的一小部分。我是初学者,所以我可能做错了什么 我的代码是: import requests link = "https://discord.gift/o2uzOR7YE3CoBpGq" r = requests.get(link) print(r.text) 输出为: <!DOCTYPE html> <

我有很多礼物,我需要创建检查器,这将检查礼物是否有效-->它将在html中搜索一些单词。我在寻找“礼物代码无效”

当我试图通过urllib或请求读取html时,它只会加载html的一小部分。我是初学者,所以我可能做错了什么

我的代码是:

import requests
link = "https://discord.gift/o2uzOR7YE3CoBpGq"
r = requests.get(link)
print(r.text)
输出为:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no" name="viewport" />

    <!-- section:seometa -->
    <meta property="og:type" content="website" />
    <meta property="og:site_name" content="Discord" />
    <meta property="og:title" content="Discord - Free voice and text chat for gamers" />
    <meta
      property="og:description"
      content="Step up your game with a modern voice & text chat app. Crystal clear voice, multiple server and channel support, mobile apps, and more. Get your free server now!"
    /><meta property="og:image" content="https://discordapp.com/assets/ee7c382d9257652a88c8f7b7f22a994d.png" />    <meta name="twitter:card" content="summary_large_image" />
    <meta name="twitter:site" content="@discordapp" />
    <meta name="twitter:creator" content="@discordapp" />
    <!-- endsection -->

    <link
      rel="chrome-webstore-item"
      href="https://chrome.google.com/webstore/detail/lcbhdgefieegnkbopmgklhlpjjdgmbog"
    />
<link rel="stylesheet" href="/assets/0.830216ebaf585f92a484.css" integrity="sha256-qzZED1N67NuVMyWOdvhIGhtLtKnOXSg+F3HcanmdW4Q= sha512-D0iS5hrftKNpXWnvjpfujnvlabUq6K5gsHbsdvctRMtQXzdf2jvZ/JwaRHAPSb9Z5Xb2o8SBeXeMTajvtrkeRw=="><link rel="icon" href="/assets/07dca80a102d4149e9736d4b162cff6f.ico" />    <!-- section:title -->
    <title>Discord</title>
    <!-- endsection -->
  </head>

  <body>
    <div id="app-mount"></div><script nonce="NjksMjM0LDU4LDI4LDkxLDUxLDYzLDE3Mg==">window.__OVERLAY__ = /overlay/.test(location.pathname)</script><script nonce="NjksMjM0LDU4LDI4LDkxLDUxLDYzLDE3Mg==">window.GLOBAL_ENV = {
      API_ENDPOINT: '//discordapp.com/api',
      WEBAPP_ENDPOINT: '//discordapp.com',
      CDN_HOST: 'cdn.discordapp.com',
      ASSET_ENDPOINT: 'https://discordapp.com',
      WIDGET_ENDPOINT: '//discordapp.com/widget',
      INVITE_HOST: 'discord.gg',
      GIFT_CODE_HOST: 'discord.gift',
      MARKETING_ENDPOINT: '//discordapp.com',
      NETWORKING_ENDPOINT: '//router.discordapp.net',
      RELEASE_CHANNEL: 'stable',
      BRAINTREE_KEY: 'production_5st77rrc_49pp2rp4phym7387',
      STRIPE_KEY: 'pk_live_CUQtlpQUF0vufWpnpUmQvcdi',
    };</script><script nonce="NjksMjM0LDU4LDI4LDkxLDUxLDYzLDE3Mg==">!function(){if(null!=window.WebSocket){var n=function(n){try{var e=localStorage.getItem(n);return null==e?null:JSON.parse(e)}catch(n){return null}},e=n("token"),o=n("gatewayURL");if(e&&o){var r=null!=window.DiscordNative||null!=window.require?"etf":"json",t=o+"/?encoding="+r+"&v=6";void 0!==window.Uint8Array&&(t+="&compress=zlib-stream"),console.log("[FAST CONNECT] "+t+", encoding: "+r+", version: 6");var a=new WebSocket(t);a.binaryType="arraybuffer";var i=Date.now(),s={open:!1,gateway:t,messages:[]};a.onopen=function(){console.log("[FAST CONNECT] connected in "+(Date.now()-i)+"ms"),s.open=!0},a.onclose=a.onerror=function(){window._ws=null},a.onmessage=function(n){s.messages.push(n)},window._ws={ws:a,state:s}}}}();</script><script src="/assets/294f56f239ff22f62fc1.js" integrity="sha256-wTRQJKoqMfG3makS9dDuuegpcHSdaGmfoEBQUPXMdDM= sha512-OVrPyjx2akoJ6QS8OZ+9blz/ADtDHruxw4gwLsjfDVUgolO1ZtcgWbOo0Zj9JBNyzAjKOSCfoFoN9lnkF0EYCw=="></script><script src="/assets/eaa48b00154d2e7ac545.js" integrity="sha256-FRTrm1gL5gkDUoKwVuL9hrrmllKXQsZg7r5zy0Xo4bo= sha512-QZ4c5JQKE5rLJf1uGLQaHHL4NpkAigt4TtluicuMZDYDE5fiL7wkaD2CMBxr0xhOO5aNfSFCxcaqBkU/xOEggQ=="></script><script src="/assets/c73d229b094bb39f0686.js" integrity="sha256-thaBLLvK6Up+B8O7zIOF9Uv8IF+gwGuOW+WUe26l/vk= sha512-5ez2fLO3oMI1UPZDif1Szfjwz04ftTNfhWWSqM81hNhuVN7kckAAZR5a1SuQG8rgsqXwN1is53uAL5M2rz/FOg=="></script>  </body>
</html>

不和
window.\uuuuuuuuuuuuuuuuuuuuuu=/OVERLAY/.test(location.pathname)window.GLOBAL\u ENV={
API_端点:'//discordapp.com/API',
WEBAPP_端点:'//discordapp.com',
CDN_主机:“CDN.discordapp.com”,
资产\终端:'https://discordapp.com',
WIDGET_端点:'//discordapp.com/WIDGET',
邀请主持人:'discord.gg',
礼物_代码_主持人:'discord.GIFT',
营销_端点:'//discordapp.com',
网络终结点:'//router.discordapp.net',
释放_通道:“稳定”,
BRAINTREE_KEY:‘生产_5st77rrc_49pp2rp4phym7387’,
条带键:“pk_live_CUQtlpQUF0vufWpnpUmQvcdi”,
};!function(){if(null!=window.WebSocket){var n=function(n){try{var e=localStorage.getItem(n);return null==e?null:JSON.parse(e)}catch(n){return null}},e=n(“token”),o=n(“gatewayURL”);if(e&&o){var r=null!=window discordinative | null!=window,console.log(“[FAST CONNECT]”“+t+”,编码:“+r+”,版本:6”);var a=new-WebSocket(t);a.binaryType=“arraybuffer”;var i=Date.now(),s={open:!1,gateway:t,messages:[]};a.onopen=function(){console.log([FAST CONNECT]连接在“+(Date.now()-i)+“ms”)中),s.open=!0},a.onclose=a.onerror=function(){window(n) },window.\uws={ws:a,state:s}}();
您可以在第一张图片中看到,该站点的html中有文本“gift code invalid”,但python输出中没有该字符串


您要查找的“礼物代码无效”可能是由js呈现的。 请求不会呈现js输出,这就是您无法找到它的原因

如果您使用的是Python3.6,请尝试使用html请求以js输出呈现网页

更新示例:

from requests_html import HTMLSession

link = 'https://discord.gift/o2uzOR7YE3CoBpGq'
targetString = "Gift Code Invalid"
session = HTMLSession()
r = session.get(link)
print("Before render is call: ", targetString in r.html.text)
# sleep has to be implemented after initial the render to get the proper response
r.html.render(wait=2, sleep=1)
print("After render is call: ", targetString in r.html.text)
输出:

Before render is call:  False
After render is call:  True
Process finished with exit code 0
您可以访问库中不同方法的文档,例如按元素查找,甚至在渲染后将响应转换为lxml对象:

该网站在后台发送一个ajax请求,并检查礼品代码的有效性。它发送一个json响应,以指示礼品代码是否有效。然后用javascript填充数据

获得所需结果的最简单方法是模拟ajax请求并获取消息。您可以不使用selenium、requests html或任何其他javascript呈现机制来实现这一点,并且仍然可以获得所需的输出—以检查礼物是否有效

import requests
gift_code='o2uzOR7YE3CoBpGq' #gift code here
link = f"https://discordapp.com/api/v6/entitlements/gift-codes/{gift_code}?with_application=true&with_subscription_plan=true"
r = requests.get(link)
print(r.json()['message'])
输出

Unknown Gift Code

到目前为止,您尝试过什么?您有没有研究过像
BeautifulSoup4
这样的网页抓取模块?这个“礼物代码无效”很可能是由javascript填充的,您应该尝试使用像
selenium
这样的浏览器自动化库,以及像
BeautifulSoup4
这样的网页抓取器