'BeautifulSoup find() takes no keyword arguments error
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')
i typed this code with watching online learning but in loop it always error. con = i.find('a', class_='sh_blog_title _sp_each_url _sp_each_title').get_text() this line show error 'find() takes no keyword arguments'
Solution 1:[1]
The problem is, you have to use .find_all() to get all <a> tags. .find() only returns one tag (if there's any):
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')
Prints:
[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
Solution 2:[2]
use .find('a', attrs={"class": "<Class name>"}) instead. Reference: Beatifulsoup docs
Solution 3:[3]
These two links will definitely help you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
| Solution 2 | sqz |
| Solution 3 | Swaroop Humane |
