我正在尝试编写一个非常简单的python / beautiful汤脚本
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.travelok.com/listings/sid.1?last_region=&searchFormText=camping'
uclient = uReq(my_url)
page_html = uclient.read()
uclient.close()
filename = "oklahoma_state_parks.csv"
f = open(filename,"w")
headers = "park_name, park_phone, \n"
f.write (headers)
page_soup = soup(page_html, 'html.parser')
containers = page_soup.findAll("article", {"class":"search-result"})
这是确保其在此处工作的测试代码。
parkinfo_name_test = containers[0].h2.text
parkinfo_phone_test = containers[0].address.div.text
parkinfo_name_test_2 = containers[2].h2.text
parkinfo_phone_test_2 = containers[2].address.div.text
如果我现在运行代码(此处的所有内容),则工作正常。 我可以打印(parkinfo_name_test)并得到第一个结果。但是当我尝试循环它时-它失败了。当我尝试使用 代码我收到一条错误消息:
for container in containers:
parkinfo_name = container[0].h2.text
parkinfo_phone = container[0].address.div.text
print ("parkinfo_name: " + parkinfo_name)
print ("parkinfo_phone " + parkinfo_phone)
f.write (parkinfo_name + "," + parkinfo_phone + "\n")
f.close ()
这是我收到的错误消息:
追溯(最近一次通话):文件“ oklahoma_state_parks.py”,第43行,位于parkinfo_name = container [0] .h2.text
文件“ C:\ Users \ Myname \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ beautifulsoup4-4.6.1-py3.7.egg \ bs4 \ element.py”,
第1071行,位于__getitem__return self.attrs [key]中
KeyError:0
答案 0 :(得分:0)
问题似乎在这里:
for container in containers:
parkinfo_name = container[0].h2.text
尝试通过这种方式进行操作:
for container in containers:
parkinfo_name = container.h2.text
parkinfo_phone = container.address.div.text
print ("parkinfo_name: " + parkinfo_name)
print ("parkinfo_phone " + parkinfo_phone)
f.write (parkinfo_name + "," + parkinfo_phone + "\n")
f.close ()