如何转换为json格式,
我收到一个错误"is not JSON serializable“
以下是我的程序
from urllib2 import urlopen as uReq
import re
from bs4 import BeautifulSoup, Comment
import requests
import json
my_url='https://uae.dubizzle.com/en/property-for-rent/residential/apartmentflat/?filters=(neighborhoods.ids=123)&page=1'
uClient=uReq(my_url)
page_html= uClient.read()
page_soup=BeautifulSoup(page_html, 'html.parser')
comments = page_soup.findAll(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]
json_output= page_soup.find_all("script",type="application/ld+json",string=re.compile("SingleFamilyResidence")) #find_all("script", "application/ld+json")
#comments = json_output.findAll(text=lambda text:isinstance(text, Comment))
#[comment.extract() for comment in comments]
#json_output.find_all(text="<script type=""application/ld+json"">").replaceWith("")
#print json_output
jsonD = json.dumps(json_output)
uClient.close()[{"@context":"http://schema.org","@type":"SingleFamilyResidence",“name”:“宽敞2BHK出租大马士革街Al Qusais","url":"https://dubai.dubizzle.com/property-for-rent/residential/apartmentflat/2018/4/29/spacious-two-bed-room-available-for-rent-i-2/","address":{"@type":"PostalAddress",”addressLocality“:”迪拜“,”addressRegion“:”迪拜“},”“:{”@type“:”产品“,”name“:”宽敞2BHK出租大马士革街Al Qusais","url":"https://dubai.dubizzle.com/property-for-rent/residential/apartmentflat/2018/4/29/spacious-two-bed-room-available-for-rent-i-2/",“优惠”:{“@type”:“优惠”,"price":49000,"priceCurrency":"AED"}},"floorSize":1400,"numberOfRooms":2,"image":"https://dbzlpvfeeds-a.akamaihd.net/images/user_images/2018/04/29/80881784_CP_photo.jpeg","geo":{"@type":"GeoCoordinates",“纬度”:55.3923,“经度”:25.2893}},{"@context":"http://schema.org","@type":"SingleFamilyResidence",“名称”:“全家具2床房公寓-Al Qusais","url":"https://dubai.dubizzle.com/property-for-rent/residential/apartmentflat/2017/10/9/fully-furnished-brand-new-2-bed-room-flat--2/","address":{"@type":"PostalAddress",”addressLocality“:”迪拜“,”addressRegion“:”迪拜“},”“:{”@type“:”产品“,”名称“:”全家具2床房公寓-Al Qusais","url":"https://dubai.dubizzle.com/property-for-rent/residential/apartmentflat/2017/10/9/fully-furnished-brand-new-2-bed-room-flat--2/","offers":{"@type":"Offer",“addressRegion”:700,"priceCurrency":"AED"}},"floorSize":1400,"numberOfRooms":2,“图像”:“https://dbzlpvfeeds-a.akamaihd.net/images/user_images/2018/09/05/84371522_CP_photo.jpeg”,“地理位置”:{“@type”:“GeoCoordinates”,“纬度”:55.3959,“经度”:25.2959}}]
发布于 2018-09-19 15:54:23
在更改为json之后,首先将bs4.element.ResultSet转换为字符串
json_data = json.dumps(str(json_output))发布于 2018-09-19 14:31:29
Hi添加了BeautifulSoup的另一个包装器,并通过
首先获取文本并使用.get_text()方法,然后使用json.loads
谢谢你,智者。
from urllib2 import urlopen as uReq
import re
from bs4 import BeautifulSoup, Comment
import requests
import json
my_url='https://uae.dubizzle.com/en/property-for-rent/residential/apartmentflat/?filters=(neighborhoods.ids=123)&page=1'
uClient=uReq(my_url)
page_html= uClient.read()
page_soup=BeautifulSoup(page_html, 'lxml')# 'html.parser')
json_output= BeautifulSoup(str(page_soup.find_all("script",type="application/ld+json",string=re.compile("SingleFamilyResidence"))), 'lxml')#find_all("script", "application/ld+json")
json_text=json_output.get_text()
json_data = json.loads(json_text)
print json_data
uClient.close()https://stackoverflow.com/questions/52392246
复制相似问题