找到列表中的项目,但是当我按索引询问位置时,它说找不到该项目



我正在编写一些代码,以便为数据库获取佛罗里达州某些县的列表。这些县列在一个网站上,但每个都在单独的网页上。为了让收集过程不那么乏味,我正在写一个webscraper。我已经得到了所有县网站的链接。我已经写了代码,然后会检查网站,找到写着";县:";然后我想知道位置,这样我就可以在下一条线路上找到县了。唯一的问题是,当我询问位置时,它说找不到。我知道它在那里,因为当我要求我的代码找到它,然后返回行(不是位置(时,它不会返回空的。我将给出一些代码供参考,并给出问题的图像。

破损代码:

links = ['https://www.ghosttowns.com/states/fl/acron.html', 'https://www.ghosttowns.com/states/fl/acton.html']
import requests
r = requests.get(links[1])
r = str(r.text)
r = r.split("n")
county_before_location = [x for x in r if 'COUNTY' in x]
print(county_before_location)
print(r.index(county_before_location))

退货:

['    </font><b><font color="#80ff80">COUNTY:</font><font color="#ffffff">'] is not in list

显示项目的代码:

links = ['https://www.ghosttowns.com/states/fl/acron.html', 'https://www.ghosttowns.com/states/fl/acton.html']
import requests
r = requests.get(links[1])
r = str(r.text)
r = r.split("n")
county_before_location = [x for x in r if 'COUNTY' in x]
print(county_before_location)

退货:

['    </font><b><font color="#80ff80">COUNTY:</font><font color="#ffffff">']

照片

county_before_location是一个列表,您要的是所述列表的索引,该索引不在r中。相反,您需要要求r.index(county_before_location[0])

最新更新