我正在尝试浏览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')
任何建议将不胜感激。 谢谢,
答案 0 :(得分:1)
zip
需要迭代器来循环,25
不是迭代器。
您可能想使用:
for container, i in zip(containers, range(25)):
如果容器的长度为25,则使用枚举:
for i, container in enumerate(containers):
答案 1 :(得分:1)