【问题标题】:Extract an object's description through Beautifulsoup in python在python中通过Beautifulsoup提取对象的描述
【发布时间】:2018-10-24 21:30:44
【问题描述】:

我想提取图形附近的描述(从“雕像模型”到“保持调谐:)”的描述)并通过 BeautifulSoup 将其存储到变量information 中。我该怎么做? 这是我的代码,但我不知道如何继续:

from bs4 import BeautifulSoup
response = requests.get('https://www.myminifactory.com/object/3d-print-the-little-prince-4707')
soup = BeautifulSoup(response.text, "lxml")
information = 

我会在我想从中提取对象描述的页面下方显示给您。先感谢您!

【问题讨论】:

  • soup.select 与 css 选择器一起使用 - 你就快到了!
  • 类似soup.find_all('div', class_='text-auto-link')

标签: python html web-scraping beautifulsoup extract


【解决方案1】:

这对我有用,因为我使用 break 语句的方式而不为脚本感到自豪。但脚本有效。

from urllib.request import urlopen
from bs4 import BeautifulSoup as BS

url = r'https://www.myminifactory.com/object/3d-print-the-little-prince-4707'

html = urlopen(url).read()
Soup = BS(html,"lxml")
Desc = Soup.find('div',{'class':'short-text text-auto-link'}).text
description = ''
for line in Desc.split('\n'):
    if line.strip() == '_________________________________________________________________________':
        break
    if line.strip():
        description += line.strip()
print(description)

【讨论】:

    【解决方案2】:

    找到父标签然后寻找<p>,过滤空格和____

    parent = soup.find("div",class_="row container-info-obj margin-t-10")
    result = [" ".join(p.text.split()) for p in parent.find_all("p") if p.text.strip() and not "_"*8  in p.text]
    #youtube_v = parent.find("iframe")["src"]
    print(result)
    

    【讨论】:

    • 这不是一个“干净”的输出(我仍然在文本中得到 '\xa0' 或其他类似的东西)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    相关资源
    最近更新 更多