解析HTML页面以获取<p>和<b>标签的内容

Rin*_*rov 5 html python beautifulsoup web-crawler

有许多HTML页面被构造为一系列这样的组:

<p>
   <b> Keywords/Category:</b>
   "keyword_a, keyword_b"
</p>
Run Code Online (Sandbox Code Playgroud)

这些页面的地址是一样https://some.page.org/year/0001,https://some.page.org/year/0002等.

如何从每个页面中分别提取关键字?我试过使用BeautifulSoup,但没有成功.我只编写了打印组标题的程序(在<b>和之间</b>).

from bs4 import BeautifulSoup
from urllib2 import urlopen
import re
html_doc = urlopen('https://some.page.org/2018/1234').read()
soup = BeautifulSoup(html_doc)
for link in soup.find_all('a'):
    print 'https://some.page.org'+link.get('href')
for node in soup.findAll('b'):
    print ''.join(node.findAll(text=True))
Run Code Online (Sandbox Code Playgroud)

Moh*_*ari 1

您需要拆分字符串,在本例中是 url/

然后你可以选择你想要的块

例如,如果 url 是https://some.page.org/year/0001我使用 split 函数用/符号分割 url

它将其转换为数组,然后我选择我需要的内容,并再次使用''.join()您可以在此链接中阅读有关 split 方法的方法将其转换为字符串