python爬虫图片代码
时间: 2023-11-12 22:55:28 浏览: 92
以下是一个Python爬虫图片的代码示例:
```python
import requests
import re
import urllib
from io import BytesIO
from PIL import Image
def get_pictures(url):
data = requests.get(url).text
regex = r'<img src="(.*?.jpg)"'
pa = re.compile(regex)
images = re.findall(pa, data)
i = 0
print('*****Starting*****')
for image in images:
if '" data-original="' in image:
address = image.split('" data-original="')[1]
else:
address = image
i += 1
image = requests.get(address)
image = Image.open(BytesIO(image.content))
image.save('D:\\111\\' + str(i) + '.jpg')
print("*****Ending*****")
if __name__ == '__main__':
url = "http://soso.nipic.com/q_中国交通and隧道景观_g_0.html"
get_pictures(url)
print("OK!!!")
```
阅读全文