我试着从房地产网站上获取身份证。在我的第一次尝试中,我总是在一个建筑项目中获得第一个不动产。现在我试着更深入地了解html树,以获得构建项目的其余部分。但我得到了一个错误:
"TypeError: list indices must be integers or slices, not str"
HTML如下所示:
"resultlist.resultlist": {
"paging": {
"next": {
"@xlink.href": "\/Suche\/S-T\/P-2\/Wohnung-Kauf\/Nordrhein-Westfalen\/Duesseldorf\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/true"
},
"current": {
"@xlink.href": "\/Suche\/S-T\/Wohnung-Kauf\/Nordrhein-Westfalen\/Duesseldorf\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/true"
},
"pageNumber": 1,
"pageSize": 20,
"numberOfPages": 3,
"numberOfHits": 140,
"numberOfListings": 50
},
"matchCountList": "",
"resultlistEntries": [{
"@numberOfHits": "140",
"@realEstateType": "2",
"resultlistEntry": [{
"@id": "111337199",
"@modification": "2019-06-09T13:36:23.513+02:00",
"@creation": "2019-05-05T14:10:47.000+02:00",
"@publishDate": "2019-05-05T14:10:47.000+02:00",
"similarObjects": [{
"similarObject": [{
"@id": "105147583",
"@modification": "2019-05-05T10:37:59.830+02:00",
"@creation": "2018-05-30T11:44:29.000+02:00",
"@publishDate": "2018-05-30T11:44:29.000+02:00",
"realEstateId": 105147583,
我的第一次尝试是这样的:
page1 = ('https://www.immobilienscout24.de/Suche/S-1/Wohnung-Kauf/Nordrhein-Westfalen/Duesseldorf/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/true')
res_page1 = requests.get(page1)
soup_page1 = bs(res_page1.content, 'lxml')
r_page1 = re.compile(r'resultListModel:(.*)')
data_page1 = soup_page1.find('script', text=r_page1).text
script_page1 = r_page1.findall(data_page1)[0].rstrip(',')
results_page1 = json.loads(script_page1)
ids_page1 = [item["@id"] for item in results_page1['searchResponseModel']['resultlist.resultlist']['resultlistEntries'][0]['resultlistEntry']]
得到这个结果:
['111353960', '110253440', '111994208', '110517626', '109984070', '109855231', '108761945', '108639776', '106997219', '106492496', '111604737', '111357085', '92741038', '112031279', '111988597', '111876292', '111870285', '111798416', '110742328', '110742299']
现在我下楼去拿剩下的也像这样:
ids_page1 = [item["@id"] for item in results_page1['searchResponseModel']['resultlist.resultlist']['resultlistEntries']['resultlistEntry']['similarObjects'][0]['similarObject']]
现在我得到的类型错误。你知道吗
有人能解释一下我做错了什么吗。你知道吗
请尝试下面这些级别
在输出中,可以看到字符串列表。注意值周围的
'
。 对一串数字调用int(s)
,将其转换为数字。你知道吗相关问题 更多 >
编程相关推荐