對關鍵詞進行搜索,使用的代碼是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;
}
}