我对Python和Scrapy非常陌生,这就是为什么我为自己创建了一个项目来学习它。但目前我正努力从以下页面获取数据:网站爬行
正如我在Chrome/Firefox的“开发工具”中所看到的那样,有8个类的表如下所示:<table class="sc-fHxwqH ddWfJE">
在这张图片中,结构和桌子我想要提取看到了我想要提取的结构和列(<td>),其中的值为=“轮椅无障碍”。值在第二列中,它是一个图片标签。它是这样读的:如果我能找到它(在这个例子中是“轮椅无障碍的”),如果我找不到它,这个值等于true,那么这个值等于false。
我管理它周围的事情,比如浏览网站的父-细节三。但是现在我无法浏览到正确的XPATH来使用class="sc-fHxwqH ddWfJE"找到这个表
我试图将其缩小到shell cmd中的基本内容:
scrapy shell 'https://www.immoscout24.ch/de/d/wohnung-kaufen-bevilard/4761145?s=2&t=2&l=436&r=40&se=16&ci=3&ct=1290'
tables = response.xpath('//*[@class="sc-fHxwqH ddWfJE"]/table')
for table in tables[1:]:
print("I found it!!") #this should be returned 8 times, once for each table
table.xpath('tr/td[1]//text()').extract_first()通往轮椅无障碍通道的完整路径是://*[@id="root"]/div/div/div[1]/section/article[7]/table/tbody/tr[1]/td[1]
不幸的是,上面的代码没有返回任何内容。我没有任何错误,但也没有我所期望的打印。
我感谢你的帮助或任何建议!我已经花了几天时间想弄清楚.
发布于 2018-02-06 20:06:28
不需要请求HTML、刮取节点值并将其放入JSON,因为所需的数据已经来自JSON格式的API
只需尝试
import requests
import json
url = "https://react-api.immoscout24.ch/v1.3/properties/4761145?ci=3&ct=1290&l=436&lng=de&p=4761145&r=40&s=2&se=16&t=2"
response = requests.get(url).json()然后您就可以获得所需的数据,例如
print(response['propertyDetails']['agency'])输出:
{'companyCity': 'Bevilard', 'companyName1': 'avendre.ch ', 'companyName2': 'Agen
ce Berne', 'companyPhoneMobile': '078 868 60 64', 'companyStreet': 'Rue Principa
le 41', 'companyZip': '2735', 'email': 'berne@avendre.ch', 'firstName': 'Verena'
, 'gender': 'f', 'lastName': 'Pecaut-Steiner', 'logoUrl': 'https://www.immoscout
24.ch/resources/memberlogos/L356353-R.jpg', 'nameFormatted': 'Verena Pecaut-Stei
ner', 'webUrl': 'http://www.avendre.ch'}发布于 2018-02-06 15:30:17
如果我正确地理解了您(用h2检查每个h2节点,后面跟着table )
for table_node in response.xpath('//article/h2/following::*[1][name()="table"]'):
#process each table herehttps://stackoverflow.com/questions/48630681
复制相似问题