Python Forum
Extract Href URL and Text From List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract Href URL and Text From List
#3
(Jul-08-2021, 11:58 AM)snippsat Wrote: Use textand attrs.
Don't use findAll('a')(will be a list) in loop just find('a')
Also use find_all('a'),the old CamleCase(do not use in Python) is kept for backward compatibility.
>>> from bs4 import BeautifulSoup
>>> 
>>> html = '<a href="/vdi-software/">VDI Software</a>'
>>> soup = BeautifulSoup(html, 'lxml')
>>> tag = soup.find('a')
>>> tag
<a href="/vdi-software/">VDI Software</a>
>>> 
>>> tag.text
'VDI Software'
>>> tag.attrs.get('href', 'Not found')
'/vdi-software/'
>>> tag.attrs.get('car', 'Not found')
'Not found'

Hi snippsat,

Thanks so much for helping out and teaching me. Also appreciate you pointing out better practices re- find_all. Awesome to learn new stuff! Big Grin
Reply


Messages In This Thread
Extract Href URL and Text From List - by knight2000 - Jul-08-2021, 11:32 AM
RE: Extract Href URL and Text From List - by knight2000 - Jul-08-2021, 12:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  BeautifulSoup pagination using href rhat398 1 2,421 Jun-30-2021, 10:55 AM
Last Post: snippsat
  Accessing a data-phone tag from an href KatMac 1 2,906 Apr-27-2021, 06:18 PM
Last Post: buran
  Selenium extract id text xzozx 1 2,136 Jun-15-2020, 06:32 AM
Last Post: Larz60+
  How to get the href value of a specific word in the html code julio2000 2 3,217 Mar-05-2020, 07:50 PM
Last Post: julio2000
  Extract text from tag content using regular expression Pavel_47 8 5,271 Nov-25-2019, 03:17 PM
Last Post: buran
  Web Scraping on href text Superzaffo 11 7,389 Nov-16-2019, 10:52 AM
Last Post: Superzaffo
  Extract text between bold headlines from HTML CostasG 1 2,338 Aug-31-2019, 10:53 AM
Last Post: snippsat
  Python/BeautiifulSoup. list of urls ->parse->extract data to csv. getting ERROR IanTheLMT 2 3,988 Jul-04-2019, 02:31 AM
Last Post: IanTheLMT
  Scrapy Picking What to Output Href or Img soothsayerpg 1 2,718 Aug-02-2018, 10:59 AM
Last Post: soothsayerpg
  Extract Anchor Text (Scrapy) soothsayerpg 2 8,348 Jul-21-2018, 07:18 AM
Last Post: soothsayerpg

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020