BeautifulSoup получает содержимое тегов <> - PullRequest
0 голосов
/ 04 марта 2020

У меня есть набор очищенных страниц, которые я должен использовать (не могу снова их обработать), которые содержат метаинформацию в цитируемых тегах &lt; &gt;, подобных этим:

 ...
 <span class="html-tag">
 &lt;meta <span class="html-attribute-name">name</span>="
 <span class="html-attribute-value">twitter:title</span>" 
 <span class="html-attribute-name">property</span>="
 <span class="html-attribute-value">og:title</span>" 
 <span class="html-attribute-name">content</span>="
 <span class="html-attribute-value">Smart TV wifi won't turn on</span>" /&gt;
 ...
 &lt;meta <span class="html-attribute-name">property</span>="
 <span class="html-attribute-value">og:url</span>" 
 <span class="html-attribute-name">content</span>="
 <span class="html-attribute-value">
 https://x.y.org/discussion/437/smart-tv-wifi-wont-turn-on</span>" /&gt;
 ...

Обновление 3 :

Эти строки, загруженные в Chrome, выглядят следующим образом:

  <meta name="twitter:title" property="og:title" content="Smart TV wifi won't turn on" />
  <meta property="og:url" content="https://x.y.org/discussion/437/lg-smart-tv-wifi-wont-turn-on" />

все же необработанный текст вместо тегов <meta> имеет &lt;meta .... &gt;meta

Можно ли получить контент из тегов &lt;meta .... &gt;meta с BeautifulSoup? Как и в этом случае, мне нужно получить "Smart TV wifi не включается" и URL " https://x.y.org/discussion/437/smart-tv-wifi-wont-turn-on "Как это сделать?

Ответы [ 2 ]

1 голос
/ 04 марта 2020

Я не знаю, если это то, что вы хотите.

from simplified_scrapy import SimplifiedDoc
html = '''
<span class="html-tag">
 &lt;meta <span class="html-attribute-name">name</span>="
 <span class="html-attribute-value">twitter:title</span>" 
 <span class="html-attribute-name">property</span>="
 <span class="html-attribute-value">og:title</span>" 
 <span class="html-attribute-name">content</span>="
 <span class="html-attribute-value">Smart TV wifi won't turn on</span>" /&gt;
 ...
 &lt;meta <span class="html-attribute-name">property</span>="
 <span class="html-attribute-value">og:url</span>" 
 <span class="html-attribute-name">content</span>="
 <span class="html-attribute-value">
 https://x.y.org/discussion/437/smart-tv-wifi-wont-turn-on</span>" /&gt;
'''

doc = SimplifiedDoc(html)
block = doc.getSectionByReg('&lt;meta[\s\S]+?/&gt;') # Get the first data block. 
span = SimplifiedDoc(block).getElementByText('content').next.text
print (span)

blocks = doc.getSectionsByReg('&lt;meta[\s\S]+?/&gt;') # Get all data blocks
for block in blocks:
    span = SimplifiedDoc(block).getElementByText('content').next.text
    print (span)

Результат:

Smart TV wifi won't turn on
Smart TV wifi won't turn on
https://x.y.org/discussion/437/smart-tv-wifi-wont-turn-on
1 голос
/ 04 марта 2020
from bs4 import BeautifulSoup


html = """ ...
 <span class="html-tag">
 &lt;meta <span class="html-attribute-name">name</span>="
 <span class="html-attribute-value">twitter:title</span>" 
 <span class="html-attribute-name">property</span>="
 <span class="html-attribute-value">og:title</span>" 
 <span class="html-attribute-name">content</span>="
 <span class="html-attribute-value">Smart TV wifi won't turn on</span>" /&gt;
 ...
 """


soup = BeautifulSoup(html, 'html.parser')

for item in soup.findAll("span", {'class': 'html-attribute-value'})[2]:
    print(item)

Обновление:

from bs4 import BeautifulSoup
import re

html = """<meta name="twitter:title" property="og:title" content="Smart TV wifi won't turn on" />
  <meta property="og:url" content="https://x.y.org/discussion/437/lg-smart-tv-wifi-wont-turn-on" />"""


soup = BeautifulSoup(html, 'html.parser')

for item in soup.findAll("meta", property=re.compile("^og")):
    print(item.get("content"))

Вывод:

Smart TV wifi won't turn on
https://x.y.org/discussion/437/lg-smart-tv-wifi-wont-turn-on
...