代码拉取完成,页面将自动刷新
import requests
import json
import jsonpath
from selenium.webdriver.common.keys import Keys
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
url = 'https://36kr.com/api/newsflash?&per_page=20'
response = requests.get(url,
headers=header,
timeout=5
)
dict = json.loads(response.text)
title=jsonpath.jsonpath(dict,"$..title]")
des=jsonpath.jsonpath(dict, '$..description')
publish=jsonpath.jsonpath(dict, '$..published_at')
for i in range(20):
print(title[i]," ",des[i], " ", publish[i])
from selenium import webdriver
wait_time=2
Max_Time_Out=10
import os
import pickle
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
option.binary_location = "/Applications/AppleTools /Google Chrome.app/Contents/MacOS/Google Chrome"
# option.add_argument('--headles')
brower = webdriver.Chrome()
wait = WebDriverWait(brower, 3)
def getTaobaoCookies():
# get login cookies
url = "https://mp.toutiao.com/profile_v3/index"
brower.get("https://mp.toutiao.com/auth/page/login/")
time.sleep(2)
username = brower.find_element_by_name("username")
username.clear()
username.send_keys("189")
brower.find_element_by_id("sso_code").click()
while True:
print("Please login in toutiao.com!")
time.sleep(3)
# if login in successfully, url jump to testerhome.com
if brower.current_url == url:
tbCookies = brower.get_cookies()
cookies = {}
for item in tbCookies:
cookies[item['name']] = item['value']
outputPath = open('Cookies.pickle', 'wb')
pickle.dump(cookies, outputPath)
outputPath.close()
# brower.close()
return cookies
print(getTaobaoCookies())
def readTaobaoCookies():
# if hava cookies file ,use it
# if not , getTaobaoCookies()
if os.path.exists('Cookies.pickle'):
readPath = open('Cookies.pickle','rb')
tbCookies = pickle.load(readPath)
else:
tbCookies = getTaobaoCookies()
return tbCookies
tbCookies = readTaobaoCookies()
print(tbCookies)
for cookie in tbCookies:
brower.add_cookie({
"domain":".tuotiao.com",
"name":cookie,
"value":tbCookies[cookie],
"path":'/',
"expires": None
})
brower.get("https://mp.toutiao.com/profile_v3/graphic/publish")
brower.refresh()
def type_toutiao(driver, title, description):
url = "https://mp.toutiao.com/profile_v3/graphic/publish"
driver.get(url)
driver.refresh()
time.sleep(5)
title_input = driver.find_element_by_tag_name("textarea")
title_input.clear()
title_input.send_keys(title)
title_input = driver.find_element_by_class_name("ProseMirror")
title_input.clear()
title_input.send_keys(description)
result=driver.find_elements_by_tag_name("input")
result[2].send_keys(Keys.SPACE)
time.sleep(1)
driver.find_element_by_id("publish").click()
time.sleep(3)
for i in range(20):
type_toutiao(brower,title[i],des[i])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。