如何确定html这些元素?
How to determine these elements of html?
在这个答案中,@Andrej Kesely 使用以下代码从 this url 的 html 中删除不必要的元素(广告、巨大 space、...)。
import requests
from bs4 import BeautifulSoup
url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')
for script in soup.select('script, .hcdcrt, #ad_contentslot_1, #ad_contentslot_2'):
script.extract()
print(soup.h2.text)
print(''.join(map(str, soup.select_one('.hom').contents)))
在我看来,那些不必要的元素被标记为script, .hcdcrt, #ad_contentslot_1, #ad_contentslot_2
。
能否请您详细说明如何查看 html 结构(通过按 F12)来确定它们?
@bigbounty 的评论解决了我的问题。我 post 在这里将我的问题从未回答列表中删除。
One way is Right Click on chrome and visualize the html DOM using livedom.validator.nu or any other online service
在这个答案中,@Andrej Kesely 使用以下代码从 this url 的 html 中删除不必要的元素(广告、巨大 space、...)。
import requests
from bs4 import BeautifulSoup
url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')
for script in soup.select('script, .hcdcrt, #ad_contentslot_1, #ad_contentslot_2'):
script.extract()
print(soup.h2.text)
print(''.join(map(str, soup.select_one('.hom').contents)))
在我看来,那些不必要的元素被标记为script, .hcdcrt, #ad_contentslot_1, #ad_contentslot_2
。
能否请您详细说明如何查看 html 结构(通过按 F12)来确定它们?
@bigbounty 的评论解决了我的问题。我 post 在这里将我的问题从未回答列表中删除。
One way is Right Click on chrome and visualize the html DOM using livedom.validator.nu or any other online service