获取目录名称

时间:2015-12-02 21:47:00

标签: php

我有这个脚本贯穿目录 images 的所有子目录,并打印出所有图像。

<?php
    $dirname = 'images/*/';
    $images  = glob($dirname . "*");
    foreach ($images as $image) {
        echo '<img src="' . $image . '" class=image /><br>';
    }
?>

我还想知道子目录的名称(&#34; *&#34;在$ dirname中),其中每个图像都是从中取出的,因此我可以将其打印出来。

所以在浏览器中它应该是这样的:

  • 目录A中的图片
  • echo&#34;此图片来自目录A&#34;
  • 目录B中的图片
  • echo&#34;此图片来自目录B&#34;

1 个答案:

答案 0 :(得分:1)

获取目录名称,然后获取其中的尾随部分:

import csv
import requests
from bs4 import BeautifulSoup
import pprint
import sys

url = 'http://www.yellowpages.com/search?search_terms=restaurants&geo_location_terms=Charleston%2C%20SC'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html, "html.parser")
g_data = soup.find_all("div", {"class": "info"}) #this isolates the big chunks of data which houses our child tags
for item in g_data: #iterates through big chunks    
    try:
        eateryName = (item.contents[0].find_all("a", {"class": "business-name"})[0].text)
    except:
        pass

    print(eateryName)
with open('csvnametest.csv', "w") as csv_file:
    writer = csv.writer(csv_file)
    writer.writerow([eateryName])