11

我正在尝试从特定的元标记中解析出内容。这是元标记的结构。前两个用反斜杠关闭,但其余的没有任何结束标记。一旦我得到第三个元标签,<head>标签之间的全部内容就会被返回。我也尝试过soup.findAll(text=re.compile('keyword')),但没有返回任何内容,因为关键字是元标记的属性。

<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="OrpXIt/y9zdAFHWzJXY2EccDi1zNSucxcCOu8+6Mc9c="/>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta content='en_US' http-equiv='Content-Language'>
<meta content='c2y_K2CiLmGeet7GUQc9e3RVGp_gCOxUC4IdJg_RBVo' name='google-site-    verification'>
<meta content='initial-scale=1.0,maximum-scale=1.0,width=device-width' name='viewport'>
<meta content='notranslate' name='google'>
<meta content="Learn about Uber's product, founders, investors and team. Everyone's Private Driver - Request a car from any mobile phone—text message, iPhone and Android apps. Within minutes, a professional driver in a sleek black car will arrive curbside. Automatically charged to your credit card on file, tip included." name='description'>

这是代码:

import csv
import re
import sys
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen

req3 = Request("https://angel.co/uber", headers={'User-Agent': 'Mozilla/5.0')
page3 = urlopen(req3).read()
soup3 = BeautifulSoup(page3)

## This returns the entire web page since the META tags are not closed
desc = soup3.findAll(attrs={"name":"description"}) 
4

6 回答 6

27

已编辑:添加了@Albert Chen 建议的区分大小写的正则表达式。

Python 3 编辑:

from bs4 import BeautifulSoup
import re
import urllib.request

page3 = urllib.request.urlopen("https://angel.co/uber").read()
soup3 = BeautifulSoup(page3)

desc = soup3.findAll(attrs={"name": re.compile(r"description", re.I)}) 
print(desc[0]['content'])

虽然我不确定它是否适用于每一页:

from bs4 import BeautifulSoup
import re
import urllib

page3 = urllib.urlopen("https://angel.co/uber").read()
soup3 = BeautifulSoup(page3)

desc = soup3.findAll(attrs={"name": re.compile(r"description", re.I)}) 
print(desc[0]['content'].encode('utf-8'))

产量:

Learn about Uber's product, founders, investors and team. Everyone's Private Dri
ver - Request a car from any mobile phoneΓÇötext message, iPhone and Android app
s. Within minutes, a professional driver in a sleek black car will arrive curbsi
de. Automatically charged to your credit card on file, tip included.
于 2013-08-08T19:59:21.297 回答
4

描述区分大小写。因此,我们需要同时查找“描述”和“描述”。

案例 1: Flipkart.com中的“描述”

案例 2: Snapdeal.com中的“描述”

from bs4 import BeautifulSoup
import requests

url= 'https://www.flipkart.com'
page3= requests.get(url)
soup3= BeautifulSoup(page3.text)
desc= soup3.find(attrs={'name':'Description'})
if desc == None:
    desc= soup3.find(attrs={'name':'description'})
try:
    print desc['content']
except Exception as e:
    print '%s (%s)' % (e.message, type(e))
于 2015-03-24T19:59:36.907 回答
4
soup3 = BeautifulSoup(page3, 'html5lib')

xhtml 要求元标记正确关闭,html5 不需要。html5lib 解析器更“宽松”。

于 2015-08-15T13:46:29.667 回答
3

尝试(基于博客文章)

from bs4 import BeautifulSoup
...
desc = ""
for meta in soup.findAll("meta"):
    metaname = meta.get('name', '').lower()
    metaprop = meta.get('property', '').lower()
    if 'description' == metaname or metaprop.find("description")>0:
        desc = meta['content'].strip()

针对以下变体进行了测试:

  • <meta name="description" content="blah blah" />
  • <meta id="MetaDescription" name="DESCRIPTION" content="blah blah" />
  • <meta property="og:description" content="blah blah" />

使用 BeautifulSoup 4.4.1版

于 2015-11-11T07:27:04.147 回答
1

我认为这里使用正则表达式应该更好:示例​​:

resp = requests.get('url')
soup = BeautifulSoup(resp.text)
desc = soup.find_all(attrs={"name": re.compile(r'Description', re.I)})
于 2017-01-22T08:13:44.933 回答
0

正如 ingo 所建议的,您可以使用不那么严格的解析器,例如 html5。

soup3 = BeautifulSoup(page3, 'html5lib')

但请确保python-html5lib系统上有可用的解析器。

于 2015-11-25T09:08:04.443 回答