Python 3.x 您好,我正在制作一个web抓取python脚本来迭代整个HTML。现在只需要第一个
我正试图从网站上截取我的学校菜单 我很难让它遍历下面的嵌套if语句。现在它只接受第一个标题、cal和info,但不会遍历HTML的其余部分。有人知道如何让if语句不断迭代吗 以下是我的python代码:Python 3.x 您好,我正在制作一个web抓取python脚本来迭代整个HTML。现在只需要第一个,python-3.x,for-loop,if-statement,beautifulsoup,screen-scraping,Python 3.x,For Loop,If Statement,Beautifulsoup,Screen Scraping,我正试图从网站上截取我的学校菜单 我很难让它遍历下面的嵌套if语句。现在它只接受第一个标题、cal和info,但不会遍历HTML的其余部分。有人知道如何让if语句不断迭代吗 以下是我的python代码: import requests from bs4 import BeautifulSoup url = "https://uc.campusdish.com/en/LocationsAndMenus/CenterCourt" page = requests.get(ur
import requests
from bs4 import BeautifulSoup
url = "https://uc.campusdish.com/en/LocationsAndMenus/CenterCourt"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html5lib')
table = soup.find_all('div', class_='menu__details')
titles = []
cals = []
infos =[]
#finds title, cals, and description in "Home Plates"
for item in table:
if item.find_all('span', class_='item__name') is not None:
title = item.a.text
titles.append(title)
cal = item.find_all('span', class_='item__calories').text
cals.append(cal)
info = item.find_all('p', class_='item__content').text
infos.append(info)
print(titles)
print(infos)
print(cals)
导入请求
从bs4导入BeautifulSoup
从prettytable导入prettytable
table=PrettyTable()
table.field_Name=['Name','carries','Content']
def主(url):
r=请求。获取(url)
soup=BeautifulSoup(r.content'html.parser')
target=soup.findAll(“li”,id=True,class=“menu\u item”)
对于目标中的焦油:
尝试:
表.添加行([tar.a.text,tar.select\u one(“span.item\u carries”)。文本,
tar.选择一个(“p.item\u content”).text])
除属性错误外:
通过
打印(表格)
主要(”https://uc.campusdish.com/en/LocationsAndMenus/CenterCourt")
当我运行这段代码时,我得到一个错误状态:文件“main.py”,第21行cal=item.find_all('span',class='item___卡路里')。文本回溯(最后一次调用):文件“main.py”,第21行,在cal=item.find_all('span',class='item___卡路里')。文本文件“/opt/virtualenvs/python3/lib/python3.8/site packages/bs4/element.py”,第2160行,在getattr raise AttributeError中(AttributeError:ResultSet对象没有属性“text”。您可能将元素列表视为单个元素。当您打算调用find()时是否调用了find_all()?是否没有收到该错误?find_all返回一个项目数组。您可以使用索引指定项目,例如item.find_all('span',class='item'uuuuu carries')[0]。text或(更好的解决方案)是使用find返回1个项目,例如:item.find('span',class='item'uuuu carries')。text
+------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| Name | Calories | Content |
+------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------+
| Grilled Lemon-Herb Pollock | 190 Calories | Grilled Pollock seasoned with lemon, garlic and oregano |
| Herb Roasted Red Potatoes | 100 Calories | Roasted quartered red potatoes tossed with rosemary, thyme and marjoram |
| Broccoli & Kale Stir-Fry | 60 Calories | Stir-fried kale, broccoli and garlic tossed with soy sauce and honey |
| White Dinner Roll | 110 Calories | Soft white dinner roll |
| Apple Betty | 200 Calories | Warm apples with a crispy brown sugar-cinnamon crumb crust |
| Chicken Caesar Salad | 330 Calories | Grilled chicken, Parmesan and seasoned croutons on a bed of crisp romaine |
| Garden Salad | 140 Calories | Egg, tomato, cucumber, carrot and bell pepper on a bed of mixed greens |
| Greek Salad | 210 Calories | Tomato, feta, cucumber, banana peppers, Kalamata olives, onion and romaine |
| Garden Salad | 15 Calories | Fresh iceberg, romaine, cucumber, plum tomato, carrot and bell pepper |
| Chocolate Chip Cookie | 120 Calories | Freshly baked chewy cookie with semisweet chocolate chips |
| Gardenburger(R) Black Bean Veggie Burger | 270 Calories | Hearty black bean burger made with brown rice, onions, corn, red bell peppers, chilies and a hint of garlic on a white roll |
| Margherita Sandwich | 420 Calories | Fresh tomato, mozzarella topped with pesto sauce & parmesan crisps on toasted whole wheat bread |
| Baked Russet Potato | 140 Calories | Oven-roasted russet potato |
| Crispy French Fries | 150 Calories | Piping hot crispy French fries |
| Sugar Cookie | 120 Calories | Freshly baked soft sugar cookie |
| Cavatappi Alfredo | 350 Calories | Cavatappi pasta tossed in creamy Alfredo sauce |
| Tossed Salad | 30 Calories | Crisp romaine lettuce tossed with fresh cucumber, carrot, onion and light Italian dressing |
| Vegan Mac & Cheese | 470 Calories | Elbow macaroni baked with creamy Daiya(R) cheddar cheese sauce and bread crumbs |
| Zucchini & Tomatoes | 40 Calories | Fresh zucchini with stewed tomatoes |
| Vegan Peach-Banana Cake | 310 Calories | Vegan blueberry banana cake topped with sweetened peaches and whipped topping |
| Chipotle Turkey Sandwich | 530 Calories | Turkey, Monterey Jack, lettuce, tomato, onion & chipotle mayo |
| Turkey, Ham & Swiss Croissant | 220 Calories | Turkey, ham, Swiss cheese, lettuce, tomato and honey mustard on a flaky croissant |
| Italian Sub | 570 Calories | Ham, salami, provolone, banana peppers, lettuce and tomato with olive oil |
| Grilled Vegetable & Hummus Wrap | 190 Calories | Grilled eggplant and zucchini, hummus, tomatoes, arugula and onion wrapped in a tortilla |
| Tandoori Chicken Wrap | 340 Calories | Tandoori-spiced chicken, pickled carrot and radish, cucumber and lettuce with curry yogurt dressing |
| Signature Chips | 140 Calories | Crispy potato chips seasoned with a blend of peppers and garlic |
| Oreo(R) Thimble Cake | 110 Calories | Moist devil's food cake with Oreo cookie pieces topped with an Oreo yogurt mousse |
| Chicken & Rice Soup Florentine | 80 Calories | Chicken, rice, spinach, carrot, celery and onion simmered in a hearty chicken broth |
| Roasted Corn Chowder | 150 Calories | Roasted corn, diced potatoes and onions in a creamy rich soup spiced with herbs and hot pepper sauce |
| Potato Leek Soup | 130 Calories | Velvety puree of potatoes, leeks, onions, celery and herbs simmered in vegetable broth |
| BBQ Pineapple-Ham Flatbread | 480 Calories | Ham, grilled pineapple and onions, Cheddar, pepper jack and BBQ sauce |
| Classic Cheese Pizza | 270 Calories | Mozzarella cheese and pizza sauce |
| Pepperoni Pizza | 300 Calories | Pepperoni, mozzarella and pizza sauce |
| Chocolate Chip Cupcake | 210 Calories | Freshly baked yellow cake topped with creamy vanilla icing and semisweet chocolate chips |
| Chocolate Chip Cookie | 120 Calories | Freshly baked chewy cookie with semisweet chocolate chips |
| Apple Stuffed Pancakes | 240 Calories | Pancakes rolled and filled with apple compote and topped with cinnamon honey yogurt drizzle |
| Hash Brown Potato Patty | 150 Calories | Crispy fried shredded potato patty |
| Chocolate Chip Cookie | 120 Calories | Freshly baked chewy cookie with semisweet chocolate chips |
| Belgian Waffle | 260 Calories | Crispy, golden-brown waffle |
+------------------------------------------+--------------+-----------------------------------------------------------------------------------------------------------------------------+