いいえ。2019年6月27日----チェック

-------------------------------------------------- --- spider_Un.py -------------------------------------------- -------------------------------------------------- ----- 
インポート要求
時間をインポート
lxmlのインポートからetree

デフget_html(URL):#请求页面
試し:
ヘッダ= {
'ユーザーエージェント':「のMozilla / 5.0(Windows NTの6.1; WOW64)のAppleWebKit / 537.36(KHTML、 Gecko)クローム/ 65.0.3325.181サファリ/ 537.36' }のような
解像度= requests.get(URL、ヘッダ=ヘッダ)
res.encoding = res.apparent_encoding
場合res.status_codeの== 200:
HTML = res.text
HTMLを返す
他:
時間を.sleep(0.1)
get_htmlリターン(URL)
E AS例外を除いて:#はBaseException除いて、このeはエラープリントアウトの原因であるかもしれ
印刷( "問題"、e)の
パス

DEFパース(HTML):
印刷(HTML)
R&LT etree.HTML =(HTML )
#Print(R&LT)
考えます。node_list = r.xpath( "// divの[クラス@ = 'コンテナ'] //スクリプト/テキスト()")[1]
印刷(考えます。node_list)
印刷(LEN(考えます。node_list))

DEFのurl_join( ):

URL_START = 'https://gaokao.chsi.com.cn/sch/search--ss-on,searchType-1,option-qg,start-'
outURL_END = '.dhtml'
URL_LIST = []
Iで用レンジ(1139):
- 20 url_num * = 20、私はある
URL = URL_START + STR(url_num)+ outURL_END
URL_LIST。(URL)を追加

url_list返す
場合__name__ == '__main__':
#url_list = url_join()
プリント(url_list)
#1
:url_listでのURLの#

##访问
#HTML = get_html(URL)

URL =「HTTPS://gaokao.chsi。 com.cn/sch/search--ss-on,searchType-1,option-qg,start-0.dhtml」
HTML = get_html(URL)
パース(HTML)
------------- -------------------------------------------------- -------------------------------------------------- --------------- weixin.py -------------------------------- -------
#Weichat 

インポートScrapy
インポートurllib.parse
news_project.middlewaresインポートDeal_Contentから
インポートNewsProjectItem news_project.itemsから
インポートetree LXMLから
インポートjs2py
インポート時間
インポート要求
インポート再
インポートBS4の

クラスWeichat(scrapy.Spider):
NAME = 'Weichat'
ベース= 'をhttps://mp.weixin.qq.com '
allowed_domains = [' weixin.sogou.com ']#ページはHTTPの定義を許可しないことをお勧めです://など
start_urlsの= [' http://weixin.sogou.com ']

マイクロチャネル
解析(自己応答)DEF:

URL_1 =' {}&https://weixin.sogou.com/weixin?type=1&query= IE = UTF8&s_from INPUT&_sug_ = Y = _ =&_sug_typeほうけい商人'形式(' )
収量scrapy.Request(URL = URL_1、コールバック= self.detail_parse、dont_filter =真)

デフdetail_parse(自己、応答):

baoji_url = response.xpath( "// [@ uigs = 'account_name_0'] / @のHREF") .extract_first()
印刷( 'baoji_url'、baoji_url)
プリント( 'baoji_url'、タイプ(baoji_url))
得scrapy.Request(URL = baoji_url、コールバック= self.baoji_parse、dont_filter =真)

デフbaoji_parse(自己、応答):

セレクタ= etree.HTML(response.text)
プリント( "------------------宝鸡招商局---------------- -----」、response.text)
スクリプト= selector.xpath( './/スクリプト[しない(@nonce)と@タイプ= "テキスト/ javascriptの"] /テキスト()')
スクリプト=スクリプト[1]
= script.replaceスクリプト( 'seajs.use( "sougou / profile.js");'、 '')
スクリプト= 'GETLIST関数(){' + +スクリプト'戻りmsgList \ N-}
スクリプトscript.replace =( 'アンプ;'、 '')
GETLIST = js2py.eval_js(スクリプト)
JSがGETLIST()=
JS = STR(JS)
JS =評価(JS)
一覧')LIS = js.get('
firstLinks = []
otherStyleTimesの= [ 】
LISにおけるLiのために:
#データベースとしてフォーマットされ、タイムスタンプに、公開された記事を取得形式で保存することができる
datimes =のLi [ 'comm_msg_info'] [ '日時']
timeArray = time.localtime(datimes)
otherStyleTime =時間。 strftime( "%Y-%M-% D%H:%のM:%のS"、timeArray)

#は、記事のURLを見つけたとき、使用後の構築実際のURL
試してみてください。
content_url = li.get( 'app_msg_ext_info')を取得( 'content_url')。
プリント(content_url)
firstLink = self.base + content_url
はIndexErrorを除き:
firstLink =なし
プリント( '!CAPTCHA')
firstLinks.append(firstLink)
otherStyleTimes .append(otherStyleTime)
プリント( 'firstLinks、otherStyleTimes ***********************'、firstLinks、otherStyleTimes)
得scrapy.Request(URL = firstLinks [0 ]、コールバック= self.baoji_detail_parse、メタ= { '時間':otherStyleTimes [0]}、dont_filter = TRUE)

DEF baoji_detail_parse(自己、応答):

アイテム= NewsProjectItem()
コンテンツ= ''
メタ= response.meta
プリント( "response.url"、response.url)
#1 res.raise_for_status()
detailPage = bs4.BeautifulSoup(response.text、 "html.parser")
获取文章标题
タイトル= detailPage .title.text.replace( '\ n'は、 '').replace( '\ rを'、 '').replace(」 ' '').replace(、 ' '!'').replace(' | 」、 '')
プリント( 'タイトル'、タイトル)

切片= detailPage.findAll( '部'、クラス_ = '_エディタ')
获取文章内容
セクションのセクションの[ - 3]:
コンテンツ=コンテンツ+ section.text .replace( 'の\ n'、 '').replace( '\のR' '')。(」」、 '')を交換

( "// divの[@ ID = 'js_content']")content_1 = response.xpath
印刷( "メタ[ '時間'] **************"、メタ[ '時間'])

印刷(「コンテンツ-------------- - 」、コンテンツ)

アイテム[ 'title_url'] = response.url

#详细页面的内容
etree = response.xpath( '// DIV [ID = @ "js_content"]')
tagContet = etree.extract()
tagContet = ' '.join(tagContet)

含有量= etree.xpath(' .//テキスト() ')。(抽出物)
含有量= '' .join(コンテンツ)
img_urls = etree.xpath(' .// IMG / @ SRC ') .extract()

img_urls_dict = {}
img_urlsにおけるURLの:
もし"HTTP://网站"でないURLに:
URL1 = urllib.parse.urljoin(response.url、URL)URLのURLの#スプライスさ
img_urls_dict [URL] = URL1

印刷( "******* img_urls_dict ****"、img_urls_dict)
項目[ 'コンテンツ']、項目[ 'タグ'] = Deal_Content.handleText(コンテンツ、tagContet、img_urls_dict、タイトル)
プリント( "** **********項目[ 'タグ'] ********************」、項目[ 'タグ'])


項目[ 'コンテンツ' ] =項目[ 'コンテンツ']置き換える。((re.findall "フォントサイズ:18px;)(*"、項目[ 'コンテンツ'])[0]、 '')

項目[ '表題'] =タイトル

項目[ '時間'] =メタ[ '時間']

#ITEM [ 'コンテンツ'] =コンテンツ

ID、PID = Deal_Content.sql_read(response.url)

項目[ 'ID' = IDの

項目[ 'PID'] = PID

項目[ 'type_cn'] = "省市级"

##Newsのニュースソース、Webサイトのホームページのことである
「宝鶏商人」アイテム[「ニュース」] =

#のtype_noがIDです
項目[ 'type_no'] = 18

収率項目
---------------------------------------- ------------------ js.py ----------------------------- -------------------------------------------------- ----------


おすすめ

転載: www.cnblogs.com/yuanjia8888/p/11099010.html