2014-07-11 64 views
0

我努力使图像刮刀,并想知道如果任何人都可以用下面的例子帮助:麻烦与我的图像刮板

page = requests.get('www.example.com/image1') 
tree = html.fromstring(page.text) 

pic = tree.xpath(Copied XPath) 

print pic[0].attrib['src'] 
现在

在“页面”我的形象在这个网址案例'www.example.com/image1'。我想知道是否可以循环这个过程中,如果我有像名称的列表例如,图像2,图像3,图像4等

回答

1

是的,这是可能的:

list_of_image_names = ['image1', 'image2', 'image3'] 

for image_name in list_of_image_names: 
    page = requests.get('www.example.com/' + image_name) 
    tree = html.fromstring(page.text) 

    pic = tree.xpath(Copied XPath) 

    print pic[0].attrib['src'] 
+0

谢谢您的回复:) – user3450524

+0

没问题。请记住,列表是可以在python中迭代的,因此不需要像@ The2ndSon所做的那样为'for'循环写入范围内的图片(len(pictureList)):' - 它比应该更复杂。 – python

0

假设上面发布的代码是可用的,您可以在某种循环中复制相同的功能。这是一个如何工作的例子。

def picLooper(): 
    pictureList = ['image1','image2', 'image3'] # list of image names 
    pictureURL = dict() # dictionary to hold URL for images 
    for picture in range(len(pictureList)): 
     page = requests.get('www.example.com/' + pictureList[picture]) 
     tree = html.fromstring(page.text) 

     pic = tree.xpath(Copied XPath) 
     pictureURL[image] = pic 

值得注意的是,这个实现假定您已经知道要获取的图像名称。希望这有助于作为一个起点! :D

+0

我有一个csv中的图像名称(超过7000)的列表。这将被定义为一个函数 – user3450524

+0

好吧,那么pictureList是事先能够生成的东西吗?听起来你知道了。 – The2ndSon

+0

非常感谢您的帮助,非常感谢。 – user3450524