web如何从此行中获取数据。。没有我找不到的div和class元素。我想从该行提取数据??如何提取

2024-05-23 15:43:21 发布

您现在位置:Python中文网/ 问答频道 /正文

<p> ==$0
  "1."the purpose of our lives is 
   to be happy." - "
   <strong>Dalai Lama</strong>
</P>

有很多类似于上面表单标签的引号,我找不到定位元素


Tags: oftheto表单isour标签be
1条回答
网友
1楼 · 发布于 2024-05-23 15:43:21
import requests
from bs4 import BeautifulSoup
from pprint import pp


def main(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'lxml')
    x = [x.get_text(strip=True, separator=" ") for x in soup.select(
        'span[data-parade-type="promoarea"] .figure_block ~ p')]

    goal = [i for i in x if i[0].isdigit()]
    pp(goal)


main('https://parade.com/937586/parade/life-quotes/')

Note, If you are using Windows machine, DO NOT forget to include from_encoding= equal to the encoding used by your sys.

参考:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#encodings

否则:

print("\n".join(goal))

相关问题 更多 >