일반적으로 원하는 콘텐츠를 웹 사이트 등에서 가져올 수있는 알고리즘을 작성하는 것은 쉽지 않습니다. 당신이 언급했듯이, 여기에 어떤 패턴도 없기 때문입니다. 어떤 사람들은 그곳에 자신의 사이트에 대한 코멘트를 달아서 comments
이나 site_comments
과 같은 클래스 이름을 붙이거나 무엇이든 여기에 넣을 수 있고 또 다른 클래스 이름을 부여 할 수 있습니다. 그래서 제가 생각하기에 클래스 이름이나 웹 사이트의 내용을 스크랩하기 위해 원하는 것을 선택해야합니다.
그럼에도 불구하고, 당신의 경우에, 당신은 BeautifulSoup's
regex 기능을 사용할 수 있다고 생각합니다.
예를 들어, 당신은 같은 것을 할 수 있습니다
from bs4 import BeautifulSoup
import requests
site_urls = [first_site, second_site]
for site in site_urls:
# this is just an example and in real life situations
# you should do some error checking
site_content = requests.get(site)
soup = BeautifulSoup(site_content, 'html5lib')
# this is the list of html tags with the current site's comments
# and you can do whatever you want with them
comments = soup.find_all(class_=re.compile("(comment)|(content)"))
그들은 documentation here 아주 좋은있다. 확인하셔야합니다.