我写过一篇刷简书访问量的方法,但是不太好。移步至刷简书访问量
这回使用python+selium模块+chrome内核
chrome+模块移步至Ubuntu使用python的selenium以及chrome模拟真实浏览器访问网页
#引入必需的模块
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
links=['https://www.jianshu.com/p/0da443d63bab','https://www.jianshu.com/p/aa89c4be9e43','https://www.jianshu.com/p/f0dcfc9cc620','https://www.jianshu.com/p/2202fb842e02','https://www.jianshu.com/p/edb79e301468','https://www.jianshu.com/p/48aa52887c9f','https://www.jianshu.com/p/97ef46be1582']
llen=len(links)
#选项
chrome_options=Options()
#设置chrome浏览器为无界面模式
chrome_options.add_argument('--headless')
#下面的两行代码一般是不需要的,至少经过我的测试在windows上是不需要的。但经过我的测试在Ubuntu上如果不加的话等下就会报错WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser,所以要加上
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
i=0
browser = webdriver.Chrome(options=chrome_options)
while i<1000:
for j in range(1,llen):
browser.get(links[j-1])
print(browser.title)
i+=1
多线程
#引入必需的模块
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import _thread
links=['https://www.jianshu.com/p/0da443d63bab','https://www.jianshu.com/p/aa89c4be9e43','https://www.jianshu.com/p/f0dcfc9cc620','https://www.jianshu.com/p/2202fb842e02','https://www.jianshu.com/p/edb79e301468','https://www.jianshu.com/p/48aa52887c9f','https://www.jianshu.com/p/97ef46be1582']
llen=len(links)
#选项
chrome_options=Options()
#设置chrome浏览器为无界面模式
chrome_options.add_argument('--headless')
#下面的两行代码一般是不需要的,至少经过我的测试在windows上是不需要的。但经过我的测试在Ubuntu上如果不加的话等下就会报错WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser,所以要加上
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
def thr(thnum):
i=0
browser = webdriver.Chrome(options=chrome_options)
while i<1000:
for j in range(0,llen):
browser.get(links[j])
print(thnum," ",i," ",j)
i+=1
try:
_thread.start_new_thread(thr,(1,))
_thread.start_new_thread(thr,(2,))
_thread.start_new_thread(thr,(3,))
_thread.start_new_thread(thr,(4,))
except:
print ("Error:无法启动线程")
while 1:
pass