对关键词进行搜索,使用的代码是python
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # 无界面
chrome_options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在报错问题
chrome_options.add_argument('--disable-gpu') # 禁用GPU硬件加速。如果软件渲染器没有就位,则GPU进程将不会启动。
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_experimental_option('mobileEmulation', {'deviceName': 'iPhone X'})
driver = webdriver.Chrome('D:/chromedriver/chromedriver.exe', options=chrome_options)
def google_search(kw, app_name):
url = "https://www.google.com/search?q=" + kw + "&prmd=nvix&source=lnms&tbm=isch&sa=X&biw=411&bih=823&dpr=3.5"
driver.get(url)
time.sleep(2)
content = driver.page_source
print(app_name)
f = open('html/' + app_name + '.html', 'w+', encoding='utf-8')
# 覆盖写入
f.write(content)
# print(content)
# driver.close()
# driver.quit()
#谷歌图片搜索 获取第一张图片
dict_arr = { "Sophia-TV-Italiano": "Sophia TV Italiano",
"Sophia-TV-Australia": "Sophia TV Australia"}
for key, value in dict_arr.items():
google_search(value, key)
下面时候如何获取第一张图片啦,由于网站是用php写的我这边使用php 读取文件下载图片
<?php
$dir ="D:/pythonDemo/html/Sky-News-Italiano.html";
$content = file_get_contents($dir);
$content = str_replace("\\","",$content);
preg_match('/jsname="DeysSe".*src="(.*)"/isU',$content,$view);
$base_content = trim($view[1]);
echo base64_image_content($base_content,"D:/phpstudy_pro/WWW/1.jpg");
function base64_image_content($base64_image_content,$file){
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
if (file_put_contents($file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
return '/'.$file;
}else{
return false;
}
}else{
return false;
}
}