http://caldining.berkeley.edu/menus/cafe3#day1
每个项目都包含在燕麦片中
但是// p [@class =" item_list"]不起作用..
答案 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_all
和select()
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)