我试图创建一些可以执行谷歌分析脚本欺骗我的网站访问的python bot。 它只适用于教育,更多不能使用。
所以这是我的代码:
import urllib
import mechanize
import socks
import socket
import time
import threading
from bs4 import BeautifulSoup
# make it like a browser
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent','chrome')]
# use proxies
...
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "....", ..., True)
socket.socket = socks.socksocket
# queries
queryip = "http://whatismyip...com/"
query = "http://www.example...com/"
# visits counter
global visitscounter
visitscounter = 0
# main function
def main():
htmltextip = br.open(queryip).read()
htmltext = br.open(query).read()
#prints
print (htmltextip)
print (htmltext)
#counter +1
global visitscounter
print 'Visits: ',visitscounter
visitscounter = visitscounter +1
# do something here ...
# call main() again in 60 seconds
threading.Timer(60, main).start()
if __name__ == "__main__":
main()
它有效,但谷歌分析不计入此次访问。 有任何想法吗?
以下是Google Analytics代码示例:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-....-1', 'example.com');
ga('send', 'pageview');
</script>
您可以在此处查看的JavaScript代码http://google-analytics.com/analytics.js
在这里让它变得美丽http://jsbeautifier.org/