Как я могу извлечь значение внутри фигурных скобок в питоне красивый суп? - PullRequest
0 голосов
/ 27 июня 2018

HTML выдержка

Это мой код:

s = BS(r.content, 'lxml')
findDiv = s.find('div', {'id':'BodyContentPlaceholder_T00DF23F8005_Col00'})
findTable = findDiv.findAll('table', {'style':'width: 100%; border-collapse: collapse;'})

for table in findTable:
        rows = table.findAll('tr')
        rows = rows[1:]
        for row in rows:
            cells = row.findAll('td')
            extension = cells[3].button.input.text
            print('docs page ' + str(extension))
            #extention = re.compile(pattern, text)
            docpage_url_ending = cells[3].find('value')
            print('url ' + str(docpage_url_ending))

Я пытаюсь получить текст navigateUrl из этого

<input id="ctl00_BodyContentPlaceholder_C013_RadListView1_ctrl1_rlb1_ClientState" name="ctl00_BodyContentPlaceholder_C013_RadListView1_ctrl1_rlb1_ClientState" type="hidden" autocomplete="off" value="{&quot;text&quot;:&quot;Documents&quot;,&quot;value&quot;:&quot;&quot;,&quot;target&quot;:&quot;&quot;,&quot;navigateUrl&quot;:&quot;/procurement/procurement-bids/constructionprocurementdetail?Title=Lakeland Adult Daycare Center Roof Replacement 18-785&quot;,&quot;primary&quot;:false}">

Ответы [ 2 ]

0 голосов
/ 27 июня 2018
import json
import bs4
import urllib

data = '<input id="ctl00_BodyContentPlaceholder_C013_RadListView1_ctrl1_rlb1_ClientState" name="ctl00_BodyContentPlaceholder_C013_RadListView1_ctrl1_rlb1_ClientState" type="hidden" autocomplete="off" value="{&quot;text&quot;:&quot;Documents&quot;,&quot;value&quot;:&quot;&quot;,&quot;target&quot;:&quot;&quot;,&quot;navigateUrl&quot;:&quot;/procurement/procurement-bids/constructionprocurementdetail?Title=Lakeland Adult Daycare Center Roof Replacement 18-785&quot;,&quot;primary&quot;:false}">'

# cook up some soup
soup = bs4.BeautifulSoup(data)
# extract the relevant attribute
vals_as_string = soup.html.body.input.attrs['value']
# it's urlencoded, so decode it
unquoted_vals_as_string = urllib.parse.unquote(vals_as_string)
# turns out, it's json
vals_as_json = json.loads(unquoted_vals_as_string)
# well, json converts to dict, so there's our target
navigateUrl = vals_as_json['navigateUrl']
0 голосов
/ 27 июня 2018

Вот как вы попадаете на navigateUrl:

import json
from bs4 import BeautifulSoup as BS

s = BS(r.content, 'lxml')
findDiv = s.find('div', {'id':'BodyContentPlaceholder_T00DF23F8005_Col00'})
findTable = findDiv.findAll('table', {'style':'width: 100%; border-collapse: collapse;'})

for table in findTable:
    rows = table.findAll('tr')
    rows = rows[1:]
    for row in rows:
        cells = row.findAll('td')
        extension = cells[3].button.input.text
        print('docs page ' + str(extension))
        #extention = re.compile(pattern, text)
        docpage_url_ending = json.loads(cells[3].button.input.attr['value'])['navigateUrl']
        print('url ' + docpage_url_ending)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...