通貨ステーションログ1 - ブロック連鎖ニュースをクロールのpython3の爬虫類
私は先に行ってきましたが、メディアサイトは、常にうんデータソースを必要とし、データソースがそれから来る、それは自分で書くか、ニュースと分析クラスメディアサイトをクロールしたいので、最近では、チェーンは火であるブロック言う事の種類の後ろに、最初に登ります。。。それは個人のプライバシーに関連していない、公開情報であるため。
ここで私最初のブロック鎖Moudingいくつかのニュースサイトであります
- 温家宝チェーン
- 8btc
- 地区と強力なメディア
- ゴールド・ファイナンス
- ファイナンス鎖
のためのコード、前記クロール集束同様のルールとしてクロール、
次クロール金金融のコードであります
import urllib.request
import json
import _thread
import threading
import time
import mysql.connector
from pyquery import PyQuery as pq
import news_base
def url_open(url):
#print(url)
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=url, headers=headers)
for i in range(10):
try:
response = urllib.request.urlopen(url=req, timeout=5).read().decode('utf-8')
return response
except :
print("chainnewscrawl except:")
def get_news(page_count, cb):
time_utc = int(time.time())
error_count = 0
index = 0
for i in range(1,page_count+1):
#print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
response = url_open("https://api.jinse.com/v6/information/list?catelogue_key=www&limit=23&information_id=%d&flag=down&version=9.9.9&_source=www"%(index))
#print(response)
json_data = json.loads(response)
for item in json_data['list']:
if item["type"] != 1 and item["type"] != 2:
continue
article_item = news_base.article_info(
item["extra"]['author'],#
int(item["extra"]["published_at"]),#
item['title'], #
item["extra"]['summary'],#
'content',
item["extra"]['topic_url'],
"金色财金")
source_responce = url_open(article_item.source_addr)
source_doc = pq(source_responce)
article_item.content = source_doc(".js-article-detail").html() if source_doc(".js-article-detail").html() else source_doc(".js-article").html()
index = item['id']
if not cb(article_item):
error_count+=1
else:
error_count = 0
if error_count >= 5:
break
if error_count >= 5:
break
#print(json_data['results'][0])
#def get_news(10)
#print(response)
いくつかの参照ライブラリについて簡単に説明する話urllib.request情報は、httpまたはhttpsのツールを使ってクロールするために使用され、失敗にリードを開くための大きなチャンスを持っています。httpクロール原因はので、ここで私は良いと感じて書きましたと機能
def url_open(url):
#print(url)
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=url, headers=headers)
for i in range(10):
try:
response = urllib.request.urlopen(url=req, timeout=5).read().decode('utf-8')
return response
except :
print("chainnewscrawl except:")
それはそれは基本的にオープンであることをバインドされた各URLがクロール達成ので、行の10回サイトを開くために、そして非常に使いやすいし続けることです
PyQueryことにjqueryのウェブサイト解析ツールに似て
mysql.connectorのための永続ストレージツールデータベース
ので、いくつかのサイトが共通のデータ構造を必要とするのでクライミングながらnews_baseとして
以下に示します。
class article_info:
def __init__(self, author, time_utc, title, desc, content, source_addr, source_media):
self.author = author
self.time_utc = time_utc
self.title = title
self.desc = desc
self.content = content
self.source_addr = source_addr
self.source_media = source_media
def __str__(self):
return("""==========================
author:%s
time_utc:%d
title:%s
desc:%s
content:%s
source_addr:%s
source_media:%s"""%(self.author, self.time_utc, self.title, self.desc, 'self.content', self.source_addr, self.source_media))
クロールプロセスのニュースは成功した結果を得るため、1つのHTTP接続によって1から搬出され、速度が非常に遅い、私が各特定のサイトのためのスレッドを開いて、ここで、幾何学的に近い速度が上がると、一緒にマルチスレッドを実行するために必要ですコードは次のとおりです。
import db_base
import news_chainfor
import news_jinse
import news_8btc
import news_55coin
import news_chainnews
import threading
class myThread (threading.Thread):
def __init__(self, func, arg1, arg2):
threading.Thread.__init__(self)
self.func = func
self.arg1 = arg1
self.arg2 = arg2
def run(self):
print ("开始线程:" + self.name)
self.func(self.arg1, self.arg2)
print ("退出线程:" + self.name)
def run():
db_base.init_db()
thread_list = [
myThread(news_55coin.get_news, 10, db_base.insert_article),
myThread(news_8btc.get_news, 10, db_base.insert_article),
myThread(news_jinse.get_news, 10, db_base.insert_article),
myThread(news_chainfor.get_news, 10, db_base.insert_article),
myThread(news_chainnews.get_news, 10, db_base.insert_article)
]
for i in range(len(thread_list)):
thread_list[i].start()
for i in range(len(thread_list)):
thread_list[i].join()
それは、これは現在の研究で、今ある前に、コードはいくつかの醜いが、私の無能かもしれ使用以下のpythonであるため、ハッハッハ
クレジットステーションがオンラインになりましたwww.bxiaozhan.com
(前後両端を含む)すべてのコード部位、位置にわたって開かれていますhttps://github.com/lihn1987/CoinCollector
私は展覧会を願っています