【问题标题】:How to get something from a webpage with python如何使用 python 从网页中获取内容
【发布时间】:2021-12-30 11:42:59
【问题描述】:

这个页面有一个网址https://www.example.com

<html>
<body>
<button id="button1" onclick=func1()>
<button id="button2" onclick=func2()>
</body>
<script>
function func1(){
  open("/doubt?s=AAAB_BCCCDD");
}

function func2(){
  open("/doubt?s=AABB_CCDDEE");
}
//something like that, it is working ....
</script>
</html>

AAAB_BCCCDDAABB_CCDDEE - 都是令牌...

我想用 python 获取页面中的第一个令牌
我的python代码-

import requests

r = requests.get("https://www.example.com")
s = r.text

if "/doubt?s=" in s:
# After this i can' understand anything ...
# i want to get the first token here as a variable

请帮帮我....

【问题讨论】:

    标签: javascript python html python-requests


    【解决方案1】:

    通常,在获取网站的原始文本内容后,您会首先使用 BeautifulSoup 之类的库来解析 HTML。它将创建一个文档对象模型 (DOM) 树,然后您可以在其中查询所需的元素。

    但是,这不会读取或解释 JavaScript 代码。对于您的问题,您可以使用regular expressions 从原始文本中提取必要的信息。

    例子:

    import re
    import requests
    
    r = requests.get("https://www.example.com")
    s = r.text
    
    pattern = re.compile('/doubt\\?s=(?P<token>\\w+)')
    matches = pattern.findall(s)
    if len(matches) > 0:
      print(matches[0])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-23
      • 2020-01-21
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2010-11-06
      相关资源
      最近更新 更多