簡體   English   中英

網頁抓取時出現 AttributeError

[英]AttributeError while web scraping

我的錯誤回溯是這樣的:

AttributeError: ResultSet object has no attribute 'find'. 
You're probably treating a list of items like a single item. 
Did you call find_all() when you meant to call find()?

我的代碼如下:

while True:
    response= requests.get(url)
    response    
    data=response.text
    soup= BeautifulSoup(data,'html.parser') 
    apis=soup.find_all('tr',{"class":"odd views-row-first"})

    for api in apis:
        name= apis.find('td',{"class":"views-field views-field-pw-version-title"}).text
        des=apis.find('td',{'class':'views-field views-field-search-api-excerpt views-field-field-api-description hidden-xs visible-md visible-sm col-md-8'}).text
        category=apis.find('td',{'class':'views-field views-field-field-article-primary-category'}).text
        link= apis.find('a',{'class':'views-field views-field-pw-version-title'}).get('href')

        print('Name:',name,'\nDescription:', des ,'\ncategory', category ,'\nLink', link)
    url_tag=soup.find('a',{'title':'Go to next page'})
    if url_tag.get('href'):
        url= url_tag.get('href')
        print(url)
    else:
        break
from bs4 import BeautifulSoup
import requests
import pandas as pd
url="https://www.programmableweb.com/category/all/apis"
while True:
    response= requests.get(url)
    response    
    data=response.text
    soup= BeautifulSoup(data,'html.parser') 
    apis=soup.find_all('tr',{"class":["odd","even"]})

    for api in apis:
        name_tag= api.find('td',{"class":"views-field views-field-pw-version-title"})
        name=name_tag.text if name_tag else 'na'
        des_tag=api.find('td',{'class':'views-field views-field-search-api-excerpt views-field-field-api-description hidden-xs visible-md visible-sm col-md-8'})
        des=des_tag.text if des_tag else 'na'
        category_tag=api.find('td',{'class':'views-field views-field-field-article-primary-category'})
        category=category_tag.text if category_tag else 'na'
        link_tag= api.find('a',{'class':'views-field views-field-pw-version-title'})
        link=link_tag.get('href') if link_tag else 'na'
        print('Name:',name,'\nDescription:', des ,'\nCategory:', category ,'\nLink:', link)
    url_tag=soup.find('a',{'title':'Go to next page'})
    if url_tag.get('href'):
        url=url+url_tag.get('href')
    else:
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM