小编宏杰李*_*宏杰李的帖子

通过beautifulsoup css选择器检索多个标签

有没有办法为 beautifulsoup 的select方法提供多个参数?

我正在通过soup.select('div[class^="TypeA"]'. 这为我获取了类与模式匹配的所有 div TypeA。此外,我有兴趣检索另一个 div where class="TypeB"(完全匹配)。

现在我可以在两个单独的通道中做到这一点,例如:

r = requests.get(jurl)
soup = BeautifulSoup(r.text,"lxml")
list1 = []
#get typeA divs
for div in soup.select('div[class^="TypeA"]'):
    t = [text for text in div.stripped_strings]
    list1.append(t)
list2 = []
#get typeB divs
for div in soup.select('div[class^="TypeB"]'):
    t = [text for text in div.stripped_strings]
    list2.append(t)
#combine the two into tuples. Both lists are of the same size
list3 = []
count = 0
for item in …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

5
推荐指数
1
解决办法
668
查看次数

使用Xpath包含ID?

<div id="content-body-14269002-17290547">
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
</div>
Run Code Online (Sandbox Code Playgroud)

我需要选择一切 id = "content-body*"

每个页面上的内容 - 主体更改,可能需要使用通配符

xpath dom scrapy

0
推荐指数
1
解决办法
9748
查看次数

标签 统计

beautifulsoup ×1

dom ×1

python ×1

scrapy ×1

xpath ×1