gpt4 book ai didi

python - 在 HTML 文件中查找字符串?

转载 作者:行者123 更新时间:2023-11-30 22:58:39 25 4
gpt4 key购买 nike

这里是Python菜鸟。我正在尝试使用 Python 打印 HTML 文件中包含子字符串的行。我知道该字符串在文件中,因为当我 ctrl+f 在 html 文件中搜索的字符串时,我找到了它。但是,当我运行代码时,它不会打印所需的结果。有人可以解释我做错了什么吗?

import requests
import datetime


from BeautifulSoup import BeautifulSoup

now =datetime.datetime.now()

cmonth = now.month
cday = now.day
cyear = now.year
find = 'boxscores/201'


url = 'http://www.basketball-reference.com/boxscores/index.cgi?lid=header_dateoutput&month={0}&day=17&year={2}'.format(cmonth,cday,cyear)
response = requests.get(url)
html = response.content
print html

for line in html:
if find in line:
print line

最佳答案

正如snakecharmerb所说,通过使用

for line in html :

当 html 是字符串时,您会迭代它的字符,而不是行。但你可以使用

for line in html.split("\n") :

迭代各行。

关于python - 在 HTML 文件中查找字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36099261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com