【发布时间】:2018-03-07 08:00:46
【问题描述】:
我尝试循环使用字典。 例如
A = {"Name":1,"Price":2}
B = {"Name":3,"Price":6}
C = {"Name":"","Price":3}
我想把 A B 和 C 加在一起
C = {"Name":[1,3,],"Price":[2,6,3]}
我该怎么做?谢谢 这是我尝试从住宿页面获取数据的实际代码。它仍然缺少我将所有数据字典放在一起并循环到下一页的部分,顺便再次感谢您。
from bs4 import BeautifulSoup as soup
import re
from selenium import webdriver
driver = webdriver.Firefox()
my_url = 'https://www.hipflat.co.th/en/search/sale/condo_y/any_r1/any_r2/any_p/any_b/any_a/any_w/any_o/any_i/100.62442610451406,13.77183154691727_c/12_z/list_v'
driver.get(my_url)
html = driver.page_source
page_soup = soup(html)
#Grab Condo
Condo = page_soup.findAll("li",{"class":"listing"})
for Con in Condo:
Name = Con.findAll("div",{"class":"listing-project"})[0].text.strip()
Description = Con.p.text
Price = Con.findAll("div",{"class":"listing-price"})[0].text.strip()
Type = Con.findAll("ul",{"class":"listing-detail"})[0].text.replace("\n","")
Type = [Name]+[Price]+[Description]+re.split(r'(bed|bath|m2)',Type)
Data = {"Name" : Name,"Description" : Description,"Price":Price,"Bed":Type[3],"Bath":Type[5],"Area(m2)":Type[7],"Floor":Type[9]}
print(Data)
next_page = driver.find_element_by_xpath('//*[@id="page-content"]/div[2]/div/div[2]/div[1]/div[2]/div/div[3]/a[1]').click()
【问题讨论】:
-
只有2个,或者还有更多的字典吗?
-
好的,我想编辑回答了我
-
好吧,我要循环添加许多字典到一个
-
我尝试将它添加到数据中,但它不起作用 Whole = [] Whole = [[Whole]+[Data]]
-
我已经拿到了字典部分。尽管如此,我还是不知道如何在不破坏页面的情况下循环页面。
标签: python-3.x loops dictionary