我想使用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