1

I try to get an HTML Element by the XPath, I tried it this way but it always returns an empty String. Can anybody tell me, how I get the Element by its X-Path ?

    for x in list_href_einrichtungen:

      url = 'https://pflegefinder.bkk-dachverband.de/' + x
      source_code = requests.get(url)
      plain_text = source_code.text
      soup = BeautifulSoup(plain_text)

      **doc = lxml.html.fromstring(source_code.content)
      strasse = doc.xpath('div[3]/div[3]/table[1]/tbody/tr/td/div/div/div[1]/p[1]/text()[1]')**

      name = soup.find('h2').text
      uebergabeeinrichtung = Einrichtung("IK", name, 'Teststraße', '12345', 'Ort', "telefon", 
      'Telefax', 'email','internet')

      list_einrichtungen.append(uebergabeeinrichtung)
      print(name,  strasse)

enter image description here

  • Does this answer your question? [can we use xpath with BeautifulSoup?](https://stackoverflow.com/questions/11465555/can-we-use-xpath-with-beautifulsoup) – Loïc Faure-Lacroix Apr 07 '20 at 02:12

1 Answers1

2

Sie können kein Element mit Beautifulsoup mit XPATH finden. Eine andere Library, die Sie benutzten können heisst 'lxml'. Das steht hier in StackOverflow. Als Beispiel:

from urllib.request import urlopen
from lxml import etree

url =  "http://www.example.com/servlet/av/ResultTemplate=AVResult.html"
response = urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
tree.xpath(xpathselector)

Ich hoffe, dass das geholfen hat!

pd: sorry fürs Deutsch, ich lerne gerade ;)

martin
  • 887
  • 7
  • 23
  • 1
    Hallo,danke für deine Antwort, ich habe es auch schon mit lxml versucht und leider klappt es damit auch nicht. CODE: response = urlopen(url) htmlparser = etree.HTMLParser() tree = etree.parse(response, htmlparser) strasse =tree.xpath('//*[@id="page"]/div[3]/div[3]/table[1]/tbody/tr/td/div/div/div[1]/p[3]/text()[1]') – SchelmEntwicklerJustin Apr 07 '20 at 01:44
  • 1
    Hast du auch mit ID-finden versucht? – martin Apr 07 '20 at 02:03