首页 > 解决方案 > beautifulsoup - 无法从列表中获取信息

问题描述

总蟒蛇业余爱好者在这里。我已经为一些表面级的东西上过几节课,但还没有为我正在尝试抓取的新网站找到解决这个问题的方法(kijiji.ca 供任何想知道的人使用)。我正在尝试为一些与博士论文相关的工作拉下有关出租房屋的信息。检查示例页面,我发现我需要的一些关键信息都具有相同的类。例如:

<div class="titleAttributes-2381855425">
  <li class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-unittype"></use></svg><span class="noLabelValue-3861810455">Condo</span></li>
  <li
    class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbedrooms"></use></svg><span class="noLabelValue-3861810455">Bedrooms: 2</span></li>
    <li
      class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbathrooms"></use></svg><span class="noLabelValue-3861810455">Bathrooms: 1</span></li>
</div>

我正在尝试获取每条信息,但是当我运行我的代码时,它什么也没有显示。

def getDetails(urls):
    urls = urls[10:]
    print(len(urls))
    i =0;
    try:
        for url in urls:
            print(url)
            listDetails = ""
            listDetailsTwo = []
            url = url.rstrip('\n')
            response = requests.get(url)
            soup = BeautifulSoup(response.text, "html.parser")
            try:
                infobar = soup.select_one("span[class*=noLabelValue-3861810455]").text
                infobar.append(infobar)
                print("Scraping listing : ",str(i))

(我知道,我的代码一定看起来一团糟,但我还是个完全的业余爱好者。)我知道我必须使用非 soup.select_one 的东西,但经过几天的尝试,我真的一无所获。任何帮助将非常感激!

谢谢!

标签: pythonweb-scrapingbeautifulsoup

解决方案


尝试这个。

from simplified_scrapy import SimplifiedDoc,req,utils
html='''
<div class="titleAttributes-2381855425">
  <li class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-unittype"></use></svg><span class="noLabelValue-3861810455">Condo</span></li>
  <li
    class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbedrooms"></use></svg><span class="noLabelValue-3861810455">Bedrooms: 2</span></li>
    <li
      class="noLabelAttribute-1492730675"><svg class="icon-459822882 attributeIcon-1499443538 attributeIcon__condensed-4247835132" focusable="false" height="100%" role="img" width="100%"><use xlink:href="#icon-attributes-numberbathrooms"></use></svg><span class="noLabelValue-3861810455">Bathrooms: 1</span></li>
</div>'''
doc = SimplifiedDoc(html)
spans = doc.selects('span.noLabelValue-3861810455>text()')
print (spans)
spans = doc.selects('div.titleAttributes-2381855425>li.noLabelAttribute-1492730675').select('span.noLabelValue-3861810455>text()')
print (spans)
utils.save2csv('test.csv',[spans])

结果:

['Condo', 'Bedrooms: 2', 'Bathrooms: 1']
['Condo', 'Bedrooms: 2', 'Bathrooms: 1']

推荐阅读