【问题标题】:beautifulsoup - Can't get info from a listbeautifulsoup - 无法从列表中获取信息
【发布时间】:2020-03-17 00:30:18
【问题描述】:

这里是 Python 爱好者。我已经为一些表面的东西上过几门课,但还没有为我正在尝试抓取的新网站找到解决这个问题的方法(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以外的东西,但经过几天的尝试,我真的无处可去。任何帮助将不胜感激!

谢谢!

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    你可能想要soup.find_all():

    L = soup.find_all('span', {'class':'noLabelValue-3861810455'})
    for item in L:
        print(item.text)
    

    打印:

    Condo
    Bedrooms: 2
    Bathrooms: 1
    

    【讨论】:

    • 太棒了,谢谢!我将所有内容都输出到一个更大的 .csv 文件中。有没有办法把它附加到那个上面?
    【解决方案2】:

    试试这个。

    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']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-31
      • 2013-04-29
      • 2016-12-19
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2022-01-14
      相关资源
      最近更新 更多