Skip to content
Advertisement

How to scrape only a single href from a div class?

I would like to extract the content of the 1st <a href> from this <div>

<div class="tocDeliverFormatsLinks"><a href="/doi/abs/10.1080/03066150.2021.1956473">Abstract</a> | <a
   class="ref nowrap full" href="/doi/full/10.1080/03066150.2021.1956473">Full Text</a> | <a
   class="ref nowrap references" href="/doi/ref/10.1080/03066150.2021.1956473">References</a> | <a
   class="ref nowrap nocolwiz" target="_blank" title="Opens new window"
   href="/doi/pdf/10.1080/03066150.2021.1956473">PDF (2239 KB)</a> | <a class="ref nowrap epub"
   href="/doi/epub/10.1080/03066150.2021.1956473" target="_blank">EPUB</a> | <a
   href="/servlet/linkout?type=rightslink&amp;url=startPage%3D1%26pageCount%3D28%26author%3DSaturnino%2BM.%2BBorras%2BJr.%252C%2B%252C%2BIan%2BScoones%252C%2Bet%2Bal%26orderBeanReset%3Dtrue%26imprint%3DRoutledge%26volumeNum%3D49%26issueNum%3D1%26contentID%3D10.1080%252F03066150.2021.1956473%26title%3DClimate%2Bchange%2Band%2Bagrarian%2Bstruggles%253A%2Ban%2Binvitation%2Bto%2Bcontribute%2Bto%2Ba%2BJPS%2BForum%26numPages%3D28%26pa%3D%26oa%3DCC-BY-NC-ND%26issn%3D0306-6150%26publisherName%3Dtandfuk%26publication%3DFJPS%26rpt%3Dn%26endPage%3D28%26publicationDate%3D01%252F02%252F2022"
   class="rightslink" target="_blank" title="Opens new window">Permissions</a>xa0</div>
<a href="/doi/abs/10.1080/03066150.2021.1956473">

I’m using BeautifulSoup and I’m also scraping some other contents from the same page and by using the following solution as result for abstract I’m having None

for article_entry in article_list_items:
    
    title_article = article_entry.find('span', class_='hlFld-Title').text
    author = article_entry.find('span', class_='articleEntryAuthorsLinks').text
    abstract = article_entry.find('a', class_='tocDeliverFormatsLinks')
      
    print(author, title_article, abstract)

Saturnino M. Borras Jr., Ian Scoones, Amita Baviskar, Marc Edelman, Nancy Lee Peluso & Wendy Wolford Climate change and agrarian struggles: an invitation to contribute to a JPS Forum None

Is there a system to reach the first href by using something similar to 'a'[:1]?

Advertisement

Answer

You can select a list then slicing or use select_one as css selector to select single element as follows:

html_doc = '''<div class="tocDeliverFormatsLinks"><a href="/doi/abs/10.1080/03066150.2021.1956473">Abstract</a> | <a
   class="ref nowrap full" href="/doi/full/10.1080/03066150.2021.1956473">Full Text</a> | <a
   class="ref nowrap references" href="/doi/ref/10.1080/03066150.2021.1956473">References</a> | <a
   class="ref nowrap nocolwiz" target="_blank" title="Opens new window"
   href="/doi/pdf/10.1080/03066150.2021.1956473">PDF (2239 KB)</a> | <a class="ref nowrap epub"
   href="/doi/epub/10.1080/03066150.2021.1956473" target="_blank">EPUB</a> | <a
   href="/servlet/linkout?type=rightslink&amp;url=startPage%3D1%26pageCount%3D28%26author%3DSaturnino%2BM.%2BBorras%2BJr.%252C%2B%252C%2BIan%2BScoones%252C%2Bet%2Bal%26orderBeanReset%3Dtrue%26imprint%3DRoutledge%26volumeNum%3D49%26issueNum%3D1%26contentID%3D10.1080%252F03066150.2021.1956473%26title%3DClimate%2Bchange%2Band%2Bagrarian%2Bstruggles%253A%2Ban%2Binvitation%2Bto%2Bcontribute%2Bto%2Ba%2BJPS%2BForum%26numPages%3D28%26pa%3D%26oa%3DCC-BY-NC-ND%26issn%3D0306-6150%26publisherName%3Dtandfuk%26publication%3DFJPS%26rpt%3Dn%26endPage%3D28%26publicationDate%3D01%252F02%252F2022"
   class="rightslink" target="_blank" title="Opens new window">Permissions</a>xa0</div>
'''
from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, 'html.parser')

href = soup.select_one('div.tocDeliverFormatsLinks a').get('href')
print(href)

Oupput:

/doi/abs/10.1080/03066150.2021.1956473
Advertisement