【发布时间】:2017-06-29 11:49:22
【问题描述】:
我正在使用 Beautiful Soup 从 html 元素中获取文本。
然后我使用循环和 if 语句将该文本与单词列表进行比较。如果它们匹配,我想返回确认。
然而,代码并没有确认任何匹配,即使打印语句显示实际上存在匹配。
def findText():
text = ""
url = 'www.site.com'
#Get url and store
page = requests.get(url)
#Get page content
soup = BeautifulSoup(page.content,"html.parser")
els = soup.select(".className")
lists = els[1].select(".className2")
for l in lists:
try:
text=l.find("li").get_text()
except(AttributeError):
text="null"
return text
def isMatch(text):
#Open csv file
listFile = open('list.csv', 'rb')
#prep file to be read
newListFile =csv.reader(listFile)
match = ""
for r in newListFile:
if r[0]==text.lower():
match = True
else:
match = False
return match
congressCSVFile.close()
匹配在输出中总是 False
print(r[0]) 在终端返回(比如说)“cat”
print(text) 在终端也返回“cat”
【问题讨论】:
标签: python csv beautifulsoup