我遇到的问题是 list_titles.append(标题)所在的行 在最底部,它提出了错误: TypeError:append()不带关键字参数。请帮助,谢谢! 每次循环我想要将项目添加到列表中。
import urllib.request
from bs4 import BeautifulSoup
import time
import sys
import pprint
list_titles = []
list_recipes = []
def user_input():
keyword = input('Please type in dinner ideas and we will give you recipes:')
return keyword
def get_html():
keyword = user_input()
search_page_url = 'http://www.foodnetwork.com/search/'
search_page = urllib.request.urlopen(search_page_url+keyword+'-') #downloads
the webpage
search_soup = BeautifulSoup(search_page,'html.parser') #create a tree data
structure out of html
### find all of the 'h3' tags with the class 'm-Med...' ....................................................
### and get the link by finding all the 'a' tags
recipe_blocks = search_soup.find_all('h3',class_='m-MediaBlock__a-
Headline')# finds h3 html tags with that class
for recipe in recipe_blocks:
recipe_url = recipe.find_all('a')[0].get('href') # get link as text
print(recipe_url)
page = urllib.request.urlopen('http:'+recipe_url)
soup = BeautifulSoup(page,'html.parser')
for name in soup.find_all('div',class_='parbase assetTitle')
[0].find_all('span',class_='o-AssetTitle__a-HeadlineText'):
title = (' '+name.get_text()
**list_titles.append(title)**
pprint.pprint(list_titles)
for ingredients in soup.find_all('div',class_='o-Ingredients__m-
Body')[0].find_all('label',class_='o-Ingredients__a-ListItemText'):
recipe = (' '+ingredients.get_text())# gets text from ingredients
list_recipes.append(recipe) # gets text from ingredients
pprint.pprint(list_recipes)