代码:
from bs4 import BeautifulSoup
import urllib.request # i use this one instead of urllib
import re
import sys
import bs4 as bs
f = open('recipe.txt', 'r')
for line in f.readlines():
wholeline = line.strip()
sauce = urllib.request.urlopen(wholeline).read()
soup = bs.BeautifulSoup(sauce, 'html.parser')
for div in soup.find_all('div', class_='nutritional_info'):
outfile = open('C:/Diabetes Scraper/ingredients.txt', 'a')
outfile.write(div.txt + '\n')
当我运行此代码时,它会给出一条错误消息:
File "C:/Diabetes Scraper/stackoverflow_scraper.py", line 14, in <module>
outfile.write(str(div.txt + '\n'))
builtins.TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
当我跑步时:
outfile.write(str(div.txt))
它在文件中给了我一堆中文符号。
当我跑步时:
outfile.write(div.txt)
它给了我错误:
File "C:/Diabetes Scraper/stackoverflow_scraper.py", line 14, in <module>
outfile.write(div.txt)
builtins.TypeError: must be str, not None
那么我该如何解决这个问题呢?我应该将它写入另一种类型的文件或以其他方式刮取数据或...? 抓取数据的示例链接:
http://www.diabetes.org/mfa-recipes/recipes/2017-03-grilled-vegetable-sandwich.html