【发布时间】:2016-08-14 13:32:35
【问题描述】:
我正在尝试使用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:title 和 og:content 那就太好了!
【问题讨论】:
标签: python html web-scraping beautifulsoup