I'm writing Python 2.7, and if you run the function below locally, it will run normally. However, if you connect to the ubuntu server and turn it directly from the terminal, the [Errno 8] Exec format error appears
The code is as follows.
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from bs4 import BeautifulSoup as bs
from datetime import datetime, timedelta
import json
from selenium.webdriver.common.keys import Keys
import requests
import random
import sys
import math
reload(sys)
sys.setdefaultencoding('utf8')
url='http://www.sohu.com/c/18/774?spm=smpc.auto-home.side-nav.14.1570682908136xGf0TUa'
driver=None
date_now=str(datetime.now())
month=date_now.split("-")[1]
day=date_now.split("-")[2].split(" ")[0]
hour=date_now.split(" ")[1].split(":")[0]
if int(day)>=10:
day='0{}'.format(int(day)-2)
else:
day='{}'.format(int(day)-2)
break_time=unicode('{}-{} {}'.format(month,day,hour))
try:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(executable_path='/home/ubuntu/chromedriver',chrome_options=chrome_options)
# # driver = webdriver.Chrome(executable_path='/Applications/chromedriver',chrome_options=chrome_options)
driver.get(url)
time.sleep(4)
last_height=driver.execute_script('return document.body.scrollHeight')
num=1
while True:
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
time.sleep(3)
psc=driver.page_source
bs4=bs(psc, ('html.parser'))
news_items=bs4.findAll('div',{'data-role':'news-item'})
news_url_list=list()
for news in news_items:
news_url=news.find('h4').find('a').get('href').replace("//","")
news_time=news.find('span',{'class':'time'}).text
print news_url
news_url_list.append(news_url)
# print 'scroll {} / '.format(num), unicode(news_time.split(":")[0]), break_time, '/ news url count: ',len(news_items)
# # print unicode(news_time.split(":")[0].split(" ")[0]) , unicode(break_time.split(" ")[0]) , int(news_time.split(":")[0].split(" ")[1]) , int(break_time.split(" ")[1])
# # print unicode(news_time.split(":")[0].split(" ")[0]) == unicode(break_time.split(" ")[0]), int(news_time.split(":")[0].split(" ")[1]) < int(break_time.split(" ")[1])
if unicode(news_time.split(":")[0].split(" ")[0]) == unicode(break_time.split(" ")[0]) and int(news_time.split(":")[0].split(" ")[1]) < int(break_time.split(" ")[1]):
break
num+=1
print 'collection news: ',len(news_url_list), 'dog'
except Exception as e:
print e
finally:
if driver is not None:
driver.quit()
sys.exit()
© 2024 OneMinuteCode. All rights reserved.