【问题标题】:BeautifulSoup find() takes no keyword arguments errorBeautifulSoup find() 没有关键字参数错误
【发布时间】:2020-07-22 03:39:06
【问题描述】:
from bs4 import BeautifulSoup
from selenium import webdriver
import time 
import sys


query_txt = input("크롤링할 내용 입력 :")


path = "C:\Temp\chromedriver_240\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get("https://www.naver.com")
time.sleep(2)

driver.find_element_by_id("query").send_keys(query_txt)
driver.find_element_by_id("search_btn").click()

driver.find_element_by_link_text("블로그 더보기").click()

full_html = driver.page_source


soup = BeautifulSoup(full_html, 'html.parser')
content_list = soup.find('ul', id='elThumbnailResultArea')
print(content_list)
content = content_list.find('a','sh_blog_title _sp_each_url _sp_each_title' ).get_text()
print(content)


for i in content_list:   
    con = i.find('a', class_='sh_blog_title _sp_each_url _sp_each_title').get_text()
    print(con)
    print('\n')

我在观看在线学习时输入了此代码,但在循环中它总是出错。 con = i.find('a', class_='sh_blog_title _sp_each_url _sp_each_title').get_text() 这一行显示错误'find() 没有关键字参数'

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:

    问题是,你必须使用.find_all() 来获取所有<a> 标签。 .find() 只返回一个标签(如果有的话):

    import requests
    from bs4 import BeautifulSoup
    
    
    url = 'https://search.naver.com/search.naver?query=tree&where=post&sm=tab_nmr&nso='
    full_html = requests.get(url).content
    
    soup = BeautifulSoup(full_html, 'html.parser')
    content_list = soup.find_all('a', class_='sh_blog_title _sp_each_url _sp_each_title' )
        
    for i in content_list:
        print(i.text)
        print('\n')
    

    打印:

    [2017/공학설계 입문] Romantic Tree
    
    
    장충동/Banyan Tree Club & Spa/Club Members Restaurant
    
    
    2020-06-27 Joshua Tree National Park Camping(조슈아트리...
    
    
    [결혼준비/D-102] 웨딩밴드 '누니주얼리 - like a tree'
    
    
    Book Club - Magic Tree House # 1 : Dinosaur Before Dark...
    
    
    비밀 정원, 조슈아 트리 국립공원(Joshua Tree National Park)
    
    
    그뤼너씨 TEA TREE 티트리 라인 3종리뷰
    
    
    Number of Nodes in the Sub-Tree With the Same Label
    
    
    태국의 100년 넘은 Giant tree
    
    
    [부산 기장 카페] 오션뷰 뷰맛집카페 : 씨앤트리 sea&tree
    

    【讨论】:

      【解决方案2】:

      请改用.find('a', attrs={"class": "<Class name>"})。参考:Beatifulsoup docs

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-12-19
          • 1970-01-01
          • 2021-04-08
          • 2021-12-22
          • 1970-01-01
          • 2020-12-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多