Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-索引器错误:列表索引超出范围-不工作_Python_Python 3.x_Web Scraping_Beautifulsoup - Fatal编程技术网

Python-索引器错误:列表索引超出范围-不工作

Python-索引器错误:列表索引超出范围-不工作,python,python-3.x,web-scraping,beautifulsoup,Python,Python 3.x,Web Scraping,Beautifulsoup,这是我的scrap.py代码 from bs4 import BeautifulSoup as soup from urllib.request import urlopen as uReq website = "https://houston.craigslist.org/search/cta" uClient = uReq(website) page_html = uClient.read() uClient.close() soup_html = soup(page_html, "h

这是我的scrap.py代码

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq

website = "https://houston.craigslist.org/search/cta"

uClient = uReq(website)
page_html = uClient.read()
uClient.close()

soup_html = soup(page_html, "html.parser")
result_html = soup_html.findAll("p", {"class":"result-info"})

filename = "products.csv"

f = open(filename, "w", encoding='utf8')
headers = "car_name, price\n"
f.write(headers)

for container in result_html:   
    carname = container.a.text

    price_container = container.findAll('span', {'class':'result-price'})
    price = price_container[0].text

    f.write(carname + "," + price + "\n")

f.close()
在终端上,它工作正常,但是当我循环它时,它会给出以下错误

Traceback (most recent call last):
  File "scrap.py", line 23, in <module>
    price = price_container[0].text.splitlines()
IndexError: list index out of range
回溯(最近一次呼叫最后一次):
文件“scrap.py”,第23行,在
price=price\u容器[0]。text.splitlines()
索引器:列表索引超出范围

请帮忙。谢谢

这是因为有些车没有价格,例如。如果没有价格,您可以将价格设置为
unknown

price_container = container.findAll('span', {'class':'result-price'})
if len(price_container) > 0:
    price = price_container[0].text
else:
    price = 'unknown'
或者您可以跳过那些没有价格的,这样它们就不会写入文件:

price_container = container.findAll('span', {'class':'result-price'})
if len(price_container) == 0:
   continue
price = price_container[0].text
我怎样才能按价格分类呢


试试下面这个。它将为您获取所有项目和价格,并处理
索引器
(如果有)

from bs4 import BeautifulSoup
from urllib.request import urlopen

response = urlopen("https://houston.craigslist.org/search/cta")
soup_html = BeautifulSoup(response.read(), "html.parser")
for container in soup_html.find_all("p", {"class":"result-info"}):   
    carname = container.find_all("a")[0].text
    try:
        price = container.find_all('span', {'class':'result-price'})[0].text
    except IndexError:
        price = ""
    print(carname,price)

我试图缩短您的代码以使其看起来更好。

太棒了。ThanksHow我可以用价格对结果排序吗?@ShahidShabbir:我添加了这个部分。但现在请停止,因为你只是要求两个家伙同时为你工作。我们如何才能获得页面上每个项目中的联系电话和电子邮件地址?正如我所说:请自己完成其余的工作,不要让这里的人为你工作。如果你想让人们为你编程,你通常需要支付他们的费用,对吗?这对你来说很简单,但对我来说很复杂,因为我是Pythonh的新手。如何用价格对结果进行排序?用现有的
print
statement
print(“车名:{}\n价格:{}\n.format(carname,price))
我投票结束这个问题,因为OP希望找到人来规划他的任务,并在每个答案上提出后续问题
from bs4 import BeautifulSoup
from urllib.request import urlopen

response = urlopen("https://houston.craigslist.org/search/cta")
soup_html = BeautifulSoup(response.read(), "html.parser")
for container in soup_html.find_all("p", {"class":"result-info"}):   
    carname = container.find_all("a")[0].text
    try:
        price = container.find_all('span', {'class':'result-price'})[0].text
    except IndexError:
        price = ""
    print(carname,price)