Python Forum
Extract tag body without leading/trailing spaces
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract tag body without leading/trailing spaces
#3
Now, the question here is, do you need to make yet another request to get the book title. And the answer is - NO
import requests
from bs4 import BeautifulSoup
 
isbn = 9783319988412
url = f'http://www.amazon.com/s?k={isbn}&ref=nb_sb_noss'
  
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0',
    'Accept': 'text/html,*/*',
    'Accept-Language': 'en,en-US;q=0.7,en;q=0.3',
    'X-Requested-With': 'XMLHttpRequest',
    'Connection': 'keep-alive'}
  
resp = requests.get(url, headers=headers)
soup = BeautifulSoup(resp.text, 'lxml')
  
# using find
a = soup.find('a', {'class': 'a-link-normal a-text-normal'})
print(a.get('href'))
print(a.text.strip())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Extract tag body without leading/trailing spaces - by buran - Nov-25-2019, 03:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [BeautifulSoup] Find </body>? Winfried 3 1,386 Jul-21-2023, 11:25 AM
Last Post: Gaurav_Kumar
  Get html body of URL rama27 6 3,514 Aug-03-2020, 02:37 PM
Last Post: snippsat
  Django- Remove leading zeros in values from database ntuttle 1 3,511 Mar-07-2019, 07:30 PM
Last Post: nilamo
  Is it possible to perform a PUT request by passing a req body instead of an ID ary 0 1,846 Feb-20-2019, 05:55 AM
Last Post: ary
  In CSV, how to write the header after writing the body? Tim 18 14,775 Jan-06-2018, 01:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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