简体   繁体   English

如何使用 BS4 和 LXML 使用 xpath

[英]How to get with xpath using BS4 and LXML

I try to get txt from website , i am using BS4 to parse website我尝试从网站获取txt ,我正在使用BS4解析网站

<ul class="sub-data-list">
<li>Monday : 11:00 - 18:00</li>
</ul>

This is source from website ( From inspect element )这是来自网站(来自检查元素)

This is my code where i trie to get data这是我尝试获取数据的代码

from bs4 import BeautifulSoup
from lxml import etree
from lxml import html
import requests

URL = "https://example.com"


webpage = requests.get(URL)
soup = BeautifulSoup(webpage.content, "html.parser")
dom = etree.HTML(str(soup))



print("საათი: :", dom.xpath('//*[@class="data-list"]')[0].text)

I also try [0].text , [1].text and etc , is there another way?我也尝试[0].text , [1].text等,还有其他方法吗?

If you are trying to get the contents of the <li> element then try something like:如果您尝试获取<li>元素的内容,请尝试以下操作:

from bs4 import BeautifulSoup

html = """<ul class="sub-data-list">
<li>Monday : 11:00 - 18:00</li>
</ul>"""

soup = BeautifulSoup(html, "html.parser")
ul = soup.find('ul', class_='sub-data-list')
print(ul.li.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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