简体   繁体   中英

Python get html element by Xpath

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)

在此处输入图像描述

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;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM