【发布时间】:2021-09-27 12:24:18
【问题描述】:
我正在尝试从网页中抓取任何b 标记之后的所有可用内容,直到下一个b 标记。为了让大家形象化,我附上相关的html elements。
以下三个是b标签Job Description,This is an internship和Qualifications的内容。所以,当我选择任何b 标签时,我会想在那个特定的b 标签和下一个b 标签之间抓取任何东西。
我试过这样:
import requests
from bs4 import BeautifulSoup
from itertools import takewhile
link = 'https://filebin.varnish-software.com/tgmdhg8dp37tycmk/doc.html'
res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
desc = [i.get_text(strip=True) for i in takewhile(lambda tag: tag.name!='b', soup.select("div > b:contains('Job Description') ~ *"))]
print(desc)
我得到的输出:
['This job entails researching developing testing and deploying mechanical solutions.', '', 'Typical activities include:', '', 'Designing and developing thermal or mechanical tooling systems.', '', 'The ideal candidate should exhibit the following behavioral traits:', '', 'Work in a technically diverse environment-Adapt to changing requirements.Verbal and written communication.Project management.', '', 'This is an internship.', 'Qualifications']
我希望得到的输出(踢掉最后两个b标签的内容):
['This job entails researching developing testing and deploying mechanical solutions.', '', 'Typical activities include:', '', 'Designing and developing thermal or mechanical tooling systems.', '', 'The ideal candidate should exhibit the following behavioral traits:', '', 'Work in a technically diverse environment-Adapt to changing requirements.Verbal and written communication.Project management.']
编辑:
这是另一个 link 供您测试。
【问题讨论】:
标签: python python-3.x web-scraping beautifulsoup