Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从DIV-beautifulsou中删除特定内容时出现问题_Python_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 从DIV-beautifulsou中删除特定内容时出现问题

Python 从DIV-beautifulsou中删除特定内容时出现问题,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我正在刮这个 我想把所有的餐馆都刮下来,这样我就可以在单独的变量中得到餐馆名称、菜肴类型和营业时间,但我不知道如何迭代它们 您可以从链接中看到,餐厅象牙赌场餐厅和LA STUB DU CASINO位于同一个分区div.infos-restos,这就是为什么我要迭代h3s,然后让下一个兄弟姐妹进去吃类型的菜肴 这是我的密码 for rests in dining_soup.select("div.infos-restos"): for rest in rests.select("

我正在刮这个

我想把所有的餐馆都刮下来,这样我就可以在单独的变量中得到餐馆名称、菜肴类型和营业时间,但我不知道如何迭代它们

您可以从链接中看到,餐厅
象牙赌场餐厅
LA STUB DU CASINO
位于同一个分区
div.infos-restos
,这就是为什么我要迭代
h3
s,然后让下一个兄弟姐妹进去吃
类型的菜肴

这是我的密码

for rests in dining_soup.select("div.infos-restos"):

        for rest in rests.select("h3"):
            print("            Rest Name: "+rest.text)
            print(rest.next_sibling.next_sibling.next_sibling.next_sibling.string)
另一个问题:):行
打印(rest.next\u sibling.next\u sibling.next\u sibling.next\u sibling.string)
打印完整的HTML。如何仅获取文本?

我建议您使用

beautifulsoup不支持
xpath

在我看来,使用
xpath

以下是您的操作方法:

from lxml import etree
import requests

url = 'http://www.accorhotels.com/gb/hotel-5548-mercure-niederbronn-hotel/restaurant.shtml'
res = requests.get(url)

tree = etree.HTML(res.content)  
rest_name_xpath = '//div[@class="infos-restos"]/div[@class="detail-resto"]/following-sibling::h3'

for item in tree.xpath(rest_name_xpath):
    print item.text
输出:

RESTAURANT DU CASINO IVORY
BAR DES MACHINES A SOUS
附言:
这个网站的html写得很糟糕,没有合适的结构。这就是为什么
xpath
又长又难看的原因

这个网站的html写得很糟糕。。我也有同样的感觉