Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.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
TypeError:zip参数#2必须支持迭代-Python 3_Python_Web Scraping - Fatal编程技术网

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

TypeError:zip参数#2必须支持迭代-Python 3,python,web-scraping,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

我正试图在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')
如有任何建议,将不胜感激。
谢谢,

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

您可能希望使用:

for container, i in zip(containers, range(25)):
如果25是容器的长度,则使用枚举:

for i, container in enumerate(containers):

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

您可能希望使用:

for container, i in zip(containers, range(25)):
如果25是容器的长度,则使用枚举:

for i, container in enumerate(containers):
将iterables作为参数,并通过将每个参数压缩在一起生成一个元组。例如
zip('abc','def')
产生
['ad','be','cf']
(我转换为一个列表)

看起来您需要
容器中每个元素的索引。在这种情况下,您可以使用:

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

看起来您需要
容器中每个元素的索引。在这种情况下,您可以使用:


你打算用
zip(containers,25)
做什么?请解释
zip(iterable,integer)
应该产生什么。也许你想用
作为container,我在enumerate(containers)
中改为enumerate(containers)
你打算用
zip(containers,25)
做什么?请解释
zip(iterable,integer)
应该产生。也许你想用
作为容器,我在enumerate(containers)
中代替?谢谢你的回复。所以我选择25,因为这是“容器”的长度,因为每页有25个列表。为什么range(容器)或len(容器)不能代替“25”?我刚刚尝试过,但再次出现错误。范围(容器)无效,范围需要数字作为参数。len(containers)只返回一个值,而不是迭代器,所以您需要对值调用range。感谢您的澄清。非常感谢您的回复。谢谢您的回复。所以我选择25,因为这是“容器”的长度,因为每页有25个列表。为什么range(容器)或len(容器)不能代替“25”?我刚刚尝试过,但再次出现错误。范围(容器)无效,范围需要数字作为参数。len(containers)只返回一个值,而不是迭代器,所以您需要对值调用range。感谢您的澄清。非常感谢您的回复。非常感谢。它现在起作用了。我将阅读枚举函数。非常感谢。它现在起作用了。我将读入枚举函数。