这个网站上菜单项的XPath是什么?

时间:2017-01-31 00:02:54

标签: python html

http://caldining.berkeley.edu/menus/cafe3#day1

每个项目都包含在燕麦片中

但是// p [@class =" item_list"]不起作用..

1 个答案:

答案 0 :(得分:0)

如果我使用xpath

,此lxml适用于我
import requests
import lxml.html

url = 'http://caldining.berkeley.edu/menus/cafe3#day1'

r = requests.get(url)

soup = lxml.html.fromstring(r.content)

for x in soup.xpath('//p[@class="item_list"]/a'):
    print(x.text)

但也许您使用BeautifulSoup没有函数xpath(),但它不会引发像"function xpath doesn't exists"这样的错误但是“TypeError:'NoneType'对象不可调用”如xpath()存在但返回None

但是有效find_allselect()

import requests
from bs4 import BeautifulSoup as BS

url = 'http://caldining.berkeley.edu/menus/cafe3#day1'

r = requests.get(url)

soup = BS(r.content, 'lxml')

for x in soup.xpath('//p[@class="item_list"]/a'): # ERROR
#for x in soup.find_all('p', class_='item_list'): # OK
#for x in soup.select('p.item_list a'): # OK
    print(x.text)