我想使用Python和美麗的湯,提取下面的標籤的內容部分meta標籤的內容屬性:獲取與BeautifulSoup和Python
<meta property="og:title" content="Super Fun Event 1" />
<meta property="og:url" content="http://superfunevents.com/events/super-fun-event-1/" />
我越來越BeautifulSoup加載頁面就好了找到其他的東西(這也抓住了隱藏在源代碼中的ID標籤的文章ID),但我不知道正確的方式來搜索HTML和找到這些位,我試過變種find和findAll無濟於事。該代碼遍歷當前的URL列表...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#importing the libraries
from urllib import urlopen
from bs4 import BeautifulSoup
def get_data(page_no):
webpage = urlopen('http://superfunevents.com/?p=' + str(i)).read()
soup = BeautifulSoup(webpage, "lxml")
for tag in soup.find_all("article") :
id = tag.get('id')
print id
# the hard part that doesn't work - I know this example is well off the mark!
title = soup.find("og:title", "content")
print (title.get_text())
url = soup.find("og:url", "content")
print (url.get_text())
# end of problem
for i in range (1,100):
get_data(i)
如果有人能幫助我整理了一下,找到了OG:標題和OG:內容會是太棒了!
有沒有內置的獲取內容,否則退回到默認? –
@ChristopheRoussy是的,這正是答案中所顯示的。另外,你可以通過使用'soup.find(「meta」,property =「og:title」,content = True)'來加強'content'屬性的存在。謝謝。 – alecxe