以下是使用Python爬取单只牙签图片的示例代码:

import requests
from bs4 import BeautifulSoup

url = "https://www.google.com/search?q=single+toothpick&rlz=1C1GCEU_enUS832US832&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiV1Jv6yI_iAhWQyDgGHaP5DgMQ_AUIDigB&biw=1366&bih=657"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}

response = requests.get(url, headers=headers)

soup = BeautifulSoup(response.content, 'html.parser')

image_tags = soup.find_all('img')

urls = []
for image in image_tags:
    urls.append(image['src'])

for url in urls:
    if 'https' in url:
        response = requests.get(url)
        with open(url.split('/')[-1], 'wb') as f:
            f.write(response.content)

这个示例代码将爬取Google上的单只牙签图片,然后将所有图片保存到当前目录下。请注意,这个代码可能会因为反爬虫机制而被禁止,请不要过度使用

爬取单只牙签图片代码python

原文地址: https://www.cveoy.top/t/topic/eDDf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录