0

So I'm trying to parse quotes from a website, but within the Result class there are multiple paragraphs. Is there a way to ignore the date and author and only select the material in quotes? So I would only be left with a list of quotes? Using BeautifulSoup btw. Thanks.

<div class="result">
  <p><strong>Date:</strong> February 2, 2019</p>
  <p>"My mind had no choice but to drift into an elaborate fantasy realm."</p>

  <blockquote>
    <p class="attribution">&mdash; Pamela, Paul</p>
  </blockquote>
  <a href="/metaphors/25249" class="load_details">preview</a> |
  <a href="/metaphors/25249" title="Let Children Get Bored Again [from The New York Times]">full record</a>
  <div class="details_container"></div>
</div>
<div class="result">
  <p><strong>Date:</strong> February 2, 2019</p>
  <p>"You let your mind wander and follow it where it goes."</p>
  <blockquote>
    <p class="attribution">&mdash; Pamela, Paul</p>
  </blockquote>
  <a href="/metaphors/25250" class="load_details">preview</a> |
  <a href="/metaphors/25250" title="Let Children Get Bored Again [from The New York Times]">full record</a>

  <div class="details_container"></div>
</div>

My current code is here:

import bs4 as bs
import urllib.request

sauce = urllib.request.urlopen('URLHERE').read()
soup = bs.BeautifulSoup(sauce,'lxml')

body = soup.body
for paragraph in body.find_all('p'):
    print(paragraph.text)
257 Aria
  • 1
  • 2

2 Answers2

1

you can use xpath for your query, for example:

import requests

from lxml import html

page = requests.get('enter_your_url')
tree = html.fromstring(page.content)
data = tree.xpath('//div[@class="result"]//p[2]/text()')

print(data)
DD_N0p
  • 229
  • 1
  • 2
  • 6
0

If I understand your question properly, you're looking to print just the quotes, which appear in every 3rd paragraph element, starting with the 2nd one.

quotes = soup.find_all('p')

for i in range(1, len(quotes), 3):
   print(quotes[i].text)

There may be a cleaner way of doing this, but that should work.

Kel Varnsen
  • 314
  • 2
  • 8
  • Ok how would you get that to print? I'm new to BS and PY. – 257 Aria Jul 10 '20 at 18:57
  • I've got it to print out the first line, but how would I do this for a whole series of quotes? – 257 Aria Jul 10 '20 at 19:08
  • To print just do `print(quote)` and `print(attribution)`. I'd have to see how the HTML looks for the series of quotes if you edit your original question to add that. – Kel Varnsen Jul 10 '20 at 19:15
  • Ok perfect. I've updated the code to include 2 entries. There are about 100 per page. – 257 Aria Jul 10 '20 at 19:23
  • I updated the answer if you want to give it a try. Note that if that webpage changes (specifically with the

    elements), the code likely won't work anymore.

    – Kel Varnsen Jul 10 '20 at 22:58