是否有 selector 可以用于(在 Python 中)到没有标签的 select 元素?
Is there a selector that can be used (in Python) to select elements without a tag?
<div id="some id" class="some class">
<table id="some other id" class="a different class">...</table>
I want this text,
<br>
this text,
<br>
along with this text
</div>
我正在尝试使用 Python 通过网络抓取具有与上述类似代码的多个页面。我尝试使用基本的 Python CSS select 或获取文本,但无法解决。我主要想知道是否有一个 select 或可以通过 Beautiful Soup select()
方法传递 selects 在 <div>
但不在 [=15] 中的元素=].我尝试 select <br>
(不知道它做了什么),但那没有用。
我对 HMTL 的了解很少,因此对于上述代码示例中的任何错误或造成的混淆,我深表歉意。
简单地删除子 table 标签可能更容易
from bs4 import BeautifulSoup as bs
html = '''
<div id="some id" class="some class">
<table id="some other id" class="a different class">not this</table>
I want this text,
<br>
this text,
<br>
along with this text
</div>
'''
soup = bs(html, 'lxml')
soup.select_one('[id="some other id"]').extract()
print(soup.select_one('[id="some id"]').text)
解决方法其实很简单。经过试验,我想通了,你可以使用下面的代码来获取上面的文本 HTML.
import requests, bs4
#Create a BeautifulSoup Object
url = 'https://url.thisisthewebsitecontainingthehtml.com'
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text)
#Create a list containing all elements with the tag <div>
divElems = soup.select('div[id="some id"]')
#Create an empty list to add the text
trueText = []
for i in divElems:
text = list(i)
trueText.append((text[-5].strip(), text[-3].strip(), text[-1].strip()))
Python的list()
函数将选中的HTML分成单独的'chunks' - <table>
标签下的所有内容,文本的第一位, <br>
标签、下一段文本、另一个 <br>
标签和最后一段文本。因为我们只想要 'chunks' 包含文本,所以我们将 text
列表的第“-1”、“-3”和“-5”元素添加到我们的 trueText
中列表。
执行此代码将创建一个列表,trueText
包含上述 HTML.
中的所需文本
<div id="some id" class="some class">
<table id="some other id" class="a different class">...</table>
I want this text,
<br>
this text,
<br>
along with this text
</div>
我正在尝试使用 Python 通过网络抓取具有与上述类似代码的多个页面。我尝试使用基本的 Python CSS select 或获取文本,但无法解决。我主要想知道是否有一个 select 或可以通过 Beautiful Soup select()
方法传递 selects 在 <div>
但不在 [=15] 中的元素=].我尝试 select <br>
(不知道它做了什么),但那没有用。
我对 HMTL 的了解很少,因此对于上述代码示例中的任何错误或造成的混淆,我深表歉意。
简单地删除子 table 标签可能更容易
from bs4 import BeautifulSoup as bs
html = '''
<div id="some id" class="some class">
<table id="some other id" class="a different class">not this</table>
I want this text,
<br>
this text,
<br>
along with this text
</div>
'''
soup = bs(html, 'lxml')
soup.select_one('[id="some other id"]').extract()
print(soup.select_one('[id="some id"]').text)
解决方法其实很简单。经过试验,我想通了,你可以使用下面的代码来获取上面的文本 HTML.
import requests, bs4
#Create a BeautifulSoup Object
url = 'https://url.thisisthewebsitecontainingthehtml.com'
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text)
#Create a list containing all elements with the tag <div>
divElems = soup.select('div[id="some id"]')
#Create an empty list to add the text
trueText = []
for i in divElems:
text = list(i)
trueText.append((text[-5].strip(), text[-3].strip(), text[-1].strip()))
Python的list()
函数将选中的HTML分成单独的'chunks' - <table>
标签下的所有内容,文本的第一位, <br>
标签、下一段文本、另一个 <br>
标签和最后一段文本。因为我们只想要 'chunks' 包含文本,所以我们将 text
列表的第“-1”、“-3”和“-5”元素添加到我们的 trueText
中列表。
执行此代码将创建一个列表,trueText
包含上述 HTML.