python - BeautifulSoup 在Python中提取没有类的值

标签 python web-scraping beautifulsoup

我想在 Python 中使用 BeautifulSoup 提取数据。

我的文档:

<div class="listing-item" data-id="309531" data-score="0">

  <div class="thumb">
    <a href="https://res.cloudinary.com/">

      <div style="background-image:url(https://res.cloudinary.com/dubizzle-com/image/upload/co_rgb:242424,l_text:oswald_140_bold_letter_spacing_5:2292,y_50/co_rgb:FFFFFF,l_text:oswald_100_bold_letter_spacing_5:01,y_-107/c_fit,w_200/abu-dhabi-plate_private-car_classic);"></div>
    </a>
  </div>
</div>

这里我想获取背景图片URL

<div style="background-image:url(https://res.cloudinary.com/dubizzle-com/image/upload/co_rgb:242424,l_text:oswald_140_bold_letter_spacing_5:2292,y_50/co_rgb:FFFFFF,l_text:oswald_100_bold_letter_spacing_5:01,y_-107/c_fit,w_200/abu-dhabi-plate_private-car_classic);"></div>

我的代码:

from textwrap import shorten
from bs4 import BeautifulSoup
from urllib.parse import parse_qsl, urljoin, urlparse
import requests

url = 'https://uae.dubizzle.com/motors/number-plates/?page={}'

print('{:^50} {:^15} {:^25} '.format('Title', 'Pice', 'Date'))

for page in range(0, 40):   # <--- Increase to number pages you want
    response = requests.get(url.format(page))
    soup = BeautifulSoup(response.text, 'lxml')

    for title, price, date, thumb  in zip(soup.select('.listing-item .title'),
                            soup.select('.listing-item .price'),
                            soup.select('.listing-item .date'),
                            soup.select('.listing-item .thumb')):

        print('{:50} {:<25} {:<15}'.format(shorten(title.get_text().strip(), 50), price.get_text().strip(), thumb.get_text().strip()))

如何从文档中获取背景图片 URL?

最佳答案

您可以通过在您的 thumb 值中搜索来访问该网址。

你可以试试这个:

代码:

from textwrap import shorten
from bs4 import BeautifulSoup
from urllib.parse import parse_qsl, urljoin, urlparse
import requests

url = 'https://uae.dubizzle.com/motors/number-plates/?page={}'

print('{:^50} {:^15} {:^25} '.format('Title', 'Price', 'Date'))

for page in range(0, 1):   # <--- Increase to number pages you want
    response = requests.get(url.format(page))
    soup = BeautifulSoup(response.text, 'lxml')

    for title, price, date, thumb  in zip(soup.select('.listing-item .title'),soup.select('.listing-item .price'),soup.select('.listing-item .date'),soup.select('.listing-item .thumb')):
        # url = thumb.find('div').get('style').split('url(')[1].split(');')[0])
        print('{:50} {:<25} {:<15}'.format(shorten(title.get_text().strip(),50),price.get_text().strip(), thumb.find('div').get('style').split('url(')[1].split(');')[0]))

关于python - BeautifulSoup 在Python中提取没有类的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59371533/

相关文章:

python - 在 64 位 Windows 上从哪里获取和安装 crypto.dll

python - 使用 Beautiful Soup 获取 'name' 属性

Python美汤选择文本

python - BeautifulSoup 获取具有特定类的 div 中所有 img 的多个元素

python - 从 python 列表项创建有序列表

Python:在没有剪贴板的情况下从 Office/Excel 文档访问嵌入式 OLE

python - 在日期时间中转换 DataFrame 列类型

node.js - 如何在 thenOpen 而不是未定义的 casper.js 中获取响应状态 404?

python - 使用 beautifulsoup 在表格的第二列中打印文本

python - 将抓取 URL 从一个蜘蛛传递到另一个蜘蛛