Home > Software engineering >  Can use Beautifulsoup to find elements hidden by other wrapped elements?
Can use Beautifulsoup to find elements hidden by other wrapped elements?

Time:12-25

I would like to extract the text data of the author affiliations on this page using Beautiful soup.

I know of a work around using selenium to simply click on the 'show more' link and scan the page again? Im not sure what kind of elements these are, hidden? as they only appear in the inspector after clicking the button.

Is there a way to extract this info just using beautiful soup or do I need selenium or something equivalent to reveal the elements in the HTML code?

from bs4 import BeautifulSoup
import requests

url = 'https://www.sciencedirect.com/science/article/abs/pii/S0920379621007596'

sp = BeautifulSoup(r.content, 'html.parser')
r = sp.get(url)
author_data = sp.find('div', id='author-group')
affiliations = author_data.find('dl', class_='affiliation').text

print(affiliations)

CodePudding user response:

That info is within a script tag though you need to map the letters for affiliations to the actual affiliations. The code below extracts the JavaScript object housing the info you want and handles with JSON library.

There is then a series of steps to dynamically determine which indices hold the info of interest and then use a constructed mapping of the letters to affiliations to assign the correct affiliation to each author.

The author first and last names are also dynamically ascertained and joined together with a space.

The intention was to avoid hardcoding indices which might change over time.


import re
import json
import requests

r = requests.get('https://www.sciencedirect.com/science/article/abs/pii/S0920379621007596',
                 headers={'User-Agent': 'Mozilla/5.0'})

data = json.loads(re.search(r'(\{"abstracts".*})', r.text).group(1))

base = [i for i in data['authors']['content']
        if i.get('#name') == 'author-group'][0]['$$']

affiliation_data = [i for i in base if i['#name'] == 'affiliation']
author_data = [i for i in base if i['#name'] == 'author']

name_info = [i['_'] for author in author_data for i in author['$$']
             if i['#name'] in ['given-name', 'surname']]

affiliations = dict(zip([j['_'] for i in affiliation_data for j in i['$$'] if j['#name'] == 'label'], [
                    j['_'] for i in affiliation_data for j in i['$$'] if isinstance(j, dict) and '_' in j and j['_'][0].isupper()]))

# print(affiliations)


author_affiliations = dict(zip([' '.join([i[0], i[1]]) for i in zip(name_info[0::2], name_info[1::2])], [
                           affiliations[j['_']] for author in author_data for i in author['$$'] if i['#name'] == 'cross-ref' for j in i['$$'] if j['_'] != '⁎']))

print(author_affiliations)
  • Related