0

I was wondering how to pull an exact line from a website or simply make it print everything where a certain word is. Example "my dog is a beautiful dog" if I use the word beautiful I want Python to locate the whole sentence and return it.

import bs4 as bs
import urllib.request

sauce = urllib.request.urlopen('http://prisleje.dk/?page_id=2').read()
soup = bs.BeautifulSoup(sauce,"html.parser")

print(soup.find("eksempler"))
Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
Daniel Saggo
  • 108
  • 1
  • 9

1 Answers1

0

You can try this

import re
import requests
from bs4 import BeautifulSoup

r = requests.get('http://prisleje.dk/?page_id=2', headers={'User-Agent': 'My Agent'})
soup = BeautifulSoup(r.text, 'html.parser')
print(soup.find(text=re.compile('eksempler')))

Output:

eksempler på sager vi kan hjælpe med

Note:

You need to pass some headers as the site is blocking the default User-Agent.

If you're not familiar with regex you can read this- Python Regular Expressions.

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40