Upload
hackstuff
View
1.584
Download
3
Embed Size (px)
Citation preview
第一式 - 先抓抓看再說import urllib2data = urllib2.urlopen(url).read() #拿到資料啦!
不管遇到什麼網站,我都一律先用urllib2來看看這是不是個簡單抓的網站,用來決定開價技術程度
第二式 - 換個姿勢import pycurlimport StringIOcurl = pycurl.Curl()curl.fp = StringIO.StringIO()curl.setopt(pycurl.URL, url)curl.setopt(crl.WRITEFUNCTION, crl.fp.write)curl.setopt(pycurl.DEBUGFUNCTION, show) #重要!不然螢幕會很吵curl.perform()data = crl.fp.getvalue() #終於拿到資料
有些網站輕微雞掰難抓,需要cookie或是agent才可以讀到網頁,懶得用cookielib或是捏header的時候可以用
第三式 - 直接開瀏覽器硬幹!from selenium import webdriverbrowser = webdriver.Chrome() #開個Chrome出來 browser.get(url)obj = browser.find_element_by_xx('button')#選擇對象obj.send_keys('HST') #Key-In資料obj.click() #按鈕!data = browser.page_source #終於拿到資料
專門用來解一些混蛋Ajax跟一些奇奇怪怪的問題
有遇到問題,開selenium就對了:)
第四式 - 毀天滅地不敢直式import virtkey #按鍵精靈v = virtkey.virtkey()v.press_unicode(ord(s))v.release_unicode(ord(s))
import pytesser #影像辨識image = Image.open('fuck.png')data = image_to_string(image) #幹他媽的終於拿到資料
希望你不要走到這一步,使出這招你可能會有生活不能自理或是對人生絕望等副作用
看個例子class Worker(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue
def run(self): while True: try: url = self.queue.get(timeout=1) except Queue.Empty: return GetData(url) self.queue.task_done()queue = Queue.Queue()threads = []for i in range(thread_num): worker = Worker(queue) worker.setDaemon(True) worker.start() threads.append(worker)
for url in urls:
方法一 - 睡覺皇帝大import timedata = urllib2.urlopen(url1).read()time.sleep(1000000000000)data = urllib2.urlopen(url2).read()
有什麼不能解決的事先來跟我睡一覺再說