如何仅使网页中的一组[[:“”:]“之间的内容吸引漂亮的汤?

时间:2019-06-11 21:20:50

标签: python web-scraping beautifulsoup python-requests

下午好!如何使Beautifulsoup仅捕获多组“ [:”和“:]”之间的内容,到目前为止,我已经将整个页面放在汤中,但是可惜它没有标签。

What it looks like so far

到目前为止,我已经尝试了几件事:

  • soup.findAll(text="[")
  • keys = soup.find("span", attrs = {"class": "objectBox objectBox-string"})

    import bs4 as bs
    import urllib.request
    
    source = urllib.request.urlopen("https://login.microsoftonline.com/common/discovery/keys").read()
    soup = bs.BeautifulSoup(source,'lxml')
    
    # ---------------------------------------------
    
    #  prior script that I was playing with trying to tackle this issue
    
    import requests
    import urllib.request
    import time
    from bs4 import BeautifulSoup
    
    # Set URL to scrape new certs from
    newcerts = "https://login.microsoftonline.com/common/discovery/keys"
    
    # Connect to the URL
    response = requests.get(newcerts)
    
    # Parse HTML and save to BeautifulSoup Object
    soup = BeautifulSoup(response.text, "html.parser")
    
    keys = soup.find("span", attrs = {"class": "objectBox objectBox-string"})
    

最终目标是从https://login.microsoftonline.com/common/discovery/keys的Azure网站检索公共PKI密钥

2 个答案:

答案 0 :(得分:1)

不确定这是否是您要抓住的东西。请尝试以下脚本:

import json
import requests

url = 'https://login.microsoftonline.com/common/discovery/keys'

res = requests.get(url)
jsonobject = json.loads(res.content)
for item in jsonobject['keys']:
    print(item['x5c'])

答案 1 :(得分:0)

您已经从该网址获得的数据已被构造为Json或python dict格式。 我会通过请求获取该数据,并使用ast将其从字符串转换为dict格式。

让我展示一个例子:

import requests, ast

# get the response data
response = requests.get("https://login.microsoftonline.com/common/discovery/keys")

#convert from string to dict with ast
my_dict = ast.literal_eval(response.text)

#see here the output info in your dict
print(my_dict)
#check that it's a dict 
print(type(my_dict))

从这里开始,您可以使用python的一些字典知识来访问每个值。