PHP Simplehtmldom:来自data-src的额外图像URL

时间:2017-11-14 20:19:19

标签: php html regex web-scraping simple-html-dom

我正在尝试使用PHP Simplehtmldom库从网站上抓取一些图像。通常,图像网址位于'src'中,但在这种情况下,图像网址可以在'data-url'中找到。我无法访问data-url值,我希望有些人可以帮助我。

假设我有以下代码:

<section id="imagesContainer">
    <div class="img">
        <img data-src="https://example.com/image1.jpg" alt="imageAlt1">
    </div>
    <div class="img">
        <img data-src="https://example.com/image2.jpg" alt="imageAlt2">
    </div>
    <div class="img">
        <img data-src="https://example.com/image3.jpg" alt="imageAlt3">
    </div>
</section>

我尝试使用以下代码从data-src中提取图像网址,但它不会返回图像网址:

foreach($html->find('#imagesContainer') as $imagesContainer) {
    foreach($imagesContainer->find('img') as $image) {
        echo $image->data-src;

    }

}

如何从data-src中提取图像网址?是否可以使用simplehtmldom或者我需要正则表达式吗?

1 个答案:

答案 0 :(得分:0)

非常感谢你的建议。此代码使用XPath,用于提取data-url的值:

Sub Change_Pos_Inf()

Dim db As DAO.Database
Dim rst As DAO.Recordset

Dim strSQL As String
Dim strSensortyp As String
Dim strNew As String

Set db = CurrentDb()

strProduct_type = "type1"
strNew = "change1"
strSQL = "SELECT Pro FROM Pro_Cons WHERE Product_type='strProduct_type';"

Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
With rst

   If .RecordCount > 0 Then

    .MoveFirst

    .Edit

    !Pro.Value = strNew

    .Update

   End If
End With
End Sub