TypeError:zip参数2必须支持迭代-Python 3

时间:2018-11-09 17:32:24

标签: python web-scraping

我正在尝试浏览Rightmove.co.uk上的网站,并将清单价格打印到excel电子表格中。对于这最后一步(打印到Excel),我认为可能需要一个双“ for”循环,但是我使用zip函数遇到TypeError。

请在下面查看我的代码:

import re
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from xlwt import Workbook, Formula


wb = Workbook()                     # Create workbook
sheet1 = wb.add_sheet('Sheet 1')    # Create sheet, name Sheet 1 etc.
wb.save('xlwt.example.xls')         # Save the workbook under the name

postcode_list = {"5E2764","5E1682","5E757"} #   Define list of strings referring to each postcode to query


#### Loop through the various postcodes  ####
for url in postcode_list:
    my_url = "https://www.rightmove.co.uk/property-to-rent/find.html?searchType=RENT&locationIdentifier=OUTCODE%"+ url +"&insId=1&radius=0.0&minPrice=&maxPrice=&minBedrooms=&maxBedrooms=&displayPropertyType=&maxDaysSinceAdded=&sortByPriceDescending=&_includeLetAgreed=on&primaryDisplayPropertyType=&secondaryDisplayPropertyType=&oldDisplayPropertyType=&oldPrimaryDisplayPropertyType=&letType=&letFurnishType=&houseFlatShare="
    print(my_url)

# Opening connection and grabbing page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()                 # Closes after reading

page_soup = soup(page_html, "html.parser")

containers = page_soup.findAll("span",{"class":"propertyCard-priceValue"})
print(len(containers))      # the number of containers on one page


for container, i in zip(containers, 25):   # loop through all containers
    price_container = container.get_text()
    sheet1.write(i,0,price_container)
    print(price_container)
    wb.save('xlwt.example.xls')

任何建议将不胜感激。 谢谢,

2 个答案:

答案 0 :(得分:1)

zip需要迭代器来循环,25不是迭代器。

您可能想使用:

for container, i in zip(containers, range(25)):

如果容器的长度为25,则使用枚举:

for i, container in enumerate(containers):

答案 1 :(得分:1)

zip将Iterables作为参数,并通过将每个拉链压缩在一起来生成一个元组。例如,zip('abc', 'def')产生['ad', 'be', 'cf'](我转换为列表)。

您似乎想要containers中每个元素的索引。在这种情况下,您可以使用enumerate

for i, container in enumerate(containers):
    ...