Python 我如何消除打字错误

Python 我如何消除打字错误,python,python-3.x,web-scraping,Python,Python 3.x,Web Scraping,我正在尝试构建一个web scraper,奇怪的是,有时代码工作,有时不工作,如果不做任何更改,这可能是网站的问题,但我如何修复它,使其始终工作 我曾多次尝试重建31号线,但无论我如何重建,它似乎都不起作用 #html解析 page_soup=soup(page_html,“html.parser”) #抓住每个公寓 containers=page_soup.findAll(“div”,“class”:“列表项容器”}) filename=“asunto.csv” f=打开(文件名为“w”) h

我正在尝试构建一个web scraper,奇怪的是,有时代码工作,有时不工作,如果不做任何更改,这可能是网站的问题,但我如何修复它,使其始终工作

我曾多次尝试重建31号线,但无论我如何重建,它似乎都不起作用

#html解析
page_soup=soup(page_html,“html.parser”)
#抓住每个公寓
containers=page_soup.findAll(“div”,“class”:“列表项容器”})
filename=“asunto.csv”
f=打开(文件名为“w”)
headers=“Kohdetta Vuokraa、Huoneistot、Talotyyppi ja Koko、Sijainti、Vapautuu、Vuokra”
f、 写入(标题)
计数=0
对于范围(1,10)内的页面:
我的url=”https://www.vuokraovi.com/vuokra-asunnot/Uusimaa?page={}&页面类型=”
对于集装箱中的集装箱:
Vuokranantaja=container.findAll(“div”,“class”:“hidden xs col-sm-3 col-4”})[0].img[“alt”]
Huoneistot=container.findAll(“li”,{class:“半粗体”})[1]。文本
Talotyyppi=container.findAll(“li”,{“class”:“半粗体”})[0]。文本
Sijainti=container.findAll(“div”,“class”:“hidden xs col-sm-4 col-3”})[0]。findAll(“span”,“class”:“address”})[0]。text.strip().replace(“\r”,”).replace(“\n”,”).replace(“,”).replace(“,”).replace(“,”).replace(“,”,”).replace(“,”)
Vapautuu=container.findAll(“div”,{“class”):“hidden xs col-sm-4 col-3”}[0]。findAll(“span”,{“class”):“showing lease container hidden xs”}[0]。li.text
Vuokra=container.findAll(“li”,{“class”:“rent”})[0].text.strip()
期望的输出是给我我想要刮的东西,但它给了我这个:

Traceback (most recent call last):
  File "C:\Users\----\Desktop\vuokraovi.py", line 31, in <module>
    Vuokranantaja = container.findAll("div", {"class":"hidden-xs col-sm-3 col-4"})[0].img["alt"]
TypeError: 'NoneType' object is not subscriptable
回溯(最近一次呼叫最后一次):
文件“C:\Users\---\Desktop\vuokraovi.py”,第31行,在
Vuokranantaja=container.findAll(“div”,“class”:“hidden xs col-sm-3 col-4”})[0].img[“alt”]
TypeError:“非类型”对象不可下标

只是有时候用findAll方法没有什么好刮的。检查文档中的异常处理:

您得到的错误,
'NoneType'不可订阅
,表示您试图通过
None
上的索引访问某些内容,例如
None[idx]


因此,在尝试访问
容器.findAll()
结果上的项
[0]
之前,您应该首先检查是否存在任何内容。

容器.findAll(“div”,“class”:“hidden xs col-sm-3 col-4”}[0]。img[“alt”]
将不返回任何内容。因此,要求元素[0]为空就是产生该错误的原因。此外,循环中的行
my_url=”https://www.vuokraovi.com/vuokra-asunnot/Uusimaa?page={}&pageType=“
被存储,但从未用于任何事情。您可以告诉我们预期的返回值以及价格、地址。。。