正则表达式

Similar documents
untitled

Microsoft Word - template.doc

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

ebook70-19

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

附件1:

3

USING MAYA ANIMATION Keyset set Maya sets partitions MEL MEL copykey cutkey pastekey scalekey snapkey keytangent bakeresults MEL Command Reference Edi

中华人民共和国时期\(1952年\)

C/C++ - 文件IO

<4D F736F F D20D6D0CEC4B7A88C57B454CABF8C57CEBBD593CEC4D28EB9A0>

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

Stata 、cURL 交互与网络爬虫 - 小花经济学术

Guide to Install SATA Hard Disks

目 錄 壹 青 輔 會 結 案 附 件 貳 活 動 計 劃 書 參 執 行 內 容 一 教 學 內 容 二 與 當 地 教 師 教 學 交 流 三 服 務 執 行 進 度 肆 執 行 成 效 一 教 學 課 程 二 與 當 地 教 師 教 學 交 流 三 服 務 滿 意 度 調 查 伍 服 務 檢

WinMDI 28

jsj0.nps

1-34

01

上图专刊2006-3AAA.doc

衡山靈學創始人 超越時代的靈學明師 許衡山 老師 許衡山老師 出生於西元 1942 年 於 1980 年代啟發先天眼竅 自證其道 了悟真理 許 老師首先發現 人人皆可開發出第三眼能力與靈性能量 並藉由系統化的研究 將種種 生命現象與宇宙真理做深入淺出的剖析 並為生命的最終意義指出一條明路 現代文明昌

슬로시티번역,더빙 등 보고서(중문)_두현.hwp

untitled

?



EP.pdf

2

Windows XP

李 老 他 自 己 却 老 是 自 称 科 员, 老 说 我 李 科 员 怎 样 怎 样, 倒 好 像 这 是 一 个 值 得 他 夸 耀 的 什 么 官 衔 一 样 他 是 我 们 这 个 衙 门 里 资 格 最 老 的 科 员, 他 自 己 却 说 是 这 个 衙 门 里 最 没 有 出 息 的

untitled

PowerPoint 演示文稿

<4D F736F F D20BAECB1A6C0F6A3BAB7C7B9ABBFAAB7A2D0D0B9C9C6B1C4BCBCAFD7CABDF0CAB9D3C3B5C4BFC9D0D0D0D4B1A8B8E62E646F63>


QUESTION BANK UPDATED FOR STUDENT ONLY.xls

第 一 节 认 识 自 我 的 意 义 一 个 人 只 有 认 识 自 我, 才 能 够 正 确 地 认 识 到 自 己 的 优 劣 势, 找 出 自 己 的 职 业 亮 点, 为 自 己 的 顺 利 求 职 推 波 助 澜 ; 一 个 人 只 有 认 识 自 我, 才 能 在 求 职 中 保 持

Front 2 Polar F11 ( ) : Polar F11 Polar F11 Polar F11 Polar (Keeps U Fit - Own Workout Program) Polar Polar F11 Polar F11 Polar F11 Polar (



User’s Manual

bbc_bond_is_back_worksheet.doc

1 1 大概思路 创建 WebAPI 创建 CrossMainController 并编写 Nuget 安装 microsoft.aspnet.webapi.cors 跨域设置路由 编写 Jquery EasyUI 界面 运行效果 2 创建 WebAPI 创建 WebAPI, 新建 -> 项目 ->

els0xu_zh_nf_v8.book Page Wednesday, June, 009 9:5 AM ELS-0/0C.8

Microsoft Word - SupplyIT manual 3_cn_david.doc

<4D F736F F D20A5FEB0EAB0AAAFC5A4A4B5A5BEC7AED5B14DB77EB873ACEC313035A67EAE61AC46B873B14DC344BA5BB3D0B74EBB73A740C476C1C9BDC6C1C9B9EAAC49AD70B5652D ADD7A5BF>

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

Microsoft Word - Final Exam Review Packet.docx

Bus Hound 5

epub83-1

Microsoft Word - linux命令及建议.doc

考試學刊第10期-內文.indd

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

208 中 南 大 学 学 报 ( 社 会 科 学 版 ) 2013 年 第 19 卷 第 6 期 节 目 录 上 卷 一 所 载 篇 名, 乃 总 目 录 中 篇 名 之 误, 正 文 卷 一 收 录 篇 名 为 月 支 使 者 玄 觉 杜 凝 妻 灌 国 婴 女 独 狐 及 吕 卿 均 五 篇

1.ai

课题调查对象:

A dissertation for Master s degree Metro Indoor Coverage Systems Analysis And Design Author s Name: Sheng Hailiang speciality: Supervisor:Prof.Li Hui,

ebook35-2

K7VT2_QIG_v3

範本檔

DR2010.doc

Business Objects 5.1 Windows BusinessObjects 1

<4D F736F F F696E74202D20312EB9FEB6FBB1F5B9A4D2B5B4F3D1A7D5E7C1BCA3BAC3E6CFF2D1D0BEBFC9FAB8B4CAD4B5C4BDE1B9B9BBAFC3E6CAD4BFBCBACBCCBDCBF7D3EBCAB5BCF92E BBCE6C8DDC4A3CABD5D>

250 Discussion of Luo Yin s Object-Chanting Poems Kuei-Fang Liu* Abstract Luo Yin s object-chanting poems have the bright artistic feature and individ

Microsoft PowerPoint - string_kruse [兼容模式]

epub 94-3

高中英文科教師甄試心得

IP505SM_manual_cn.doc

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

06-4.indd

南華大學數位論文

科学计算的语言-FORTRAN95

背 景 资 料 对 于 在 华 经 营 的 企 业 里, 人 力 资 源 管 理 绝 不 是 一 件 轻 松 的 工 作 HR 从 业 者 除 了 要 具 备 猎 人 的 眼 光 心 理 学 家 的 耐 心 谈 判 专 家 的 口 才, 更 为 重 要 的 是, 还 需 要 具 备 专 业 的 法


2

國家圖書館典藏電子全文

W. Richard Stevens UNIX Sockets API echo Sockets TCP OOB IO C struct C/C++ UNIX fork() select(2)/poll(2)/epoll(4) IO IO CPU 100% libevent UNIX CPU IO

影視後製全攻略 Premiere Pro After Effects Encore 自序 Adobe Premiere Pro After Effects Encore 2008 Adobe CS Adobe CS5 Adobe CS4 Premiere Pro After Effect

audiogram3 Owners Manual

<4D F736F F D20B2F8A74AA4AF5FA578C657A175BCC6A6ECB6D7AC79A176BB50A46AB3B0A175A454BAF4A658A440A176AC46B5A6A641B1B4>


第一章

Open topic Bellman-Ford算法与负环

Transcription:

2017-09-17 爬虫俱乐部 1

*cntrade: 下载股票交易数据 *chinafin: 下载上市公司财务数据 cnintraday: 下载上市公司分时交易数据 *cnstock: 下载股票代码 *chinagcode 与 chinaaddress: 通过百度地图 API 将中文地址与经纬度相互转换 *subinfile: 修改文本文件 *wordcovert: docx doc rtf pdf 等文件之间相互转换 psemail: 发送邮件 eventstudy: 事件研究 ttable2: 分组 t 检验 * 结果输出 :reg2docx sum2docx corr2docx t2docx 即将发布 : table2docx fillup wordcloud mapscatter 2017-09-17 爬虫俱乐部 2

最初目的 : 批量修改 do 文件路径 名字由来 :subinstr() substitute in file 功能 :1 保留含有指定字符串或匹配到正则表达式的行 2 替换指定的子字符串或正则表达式匹配到的子字符串 3 删除空行 意外发现 : 简化处理源代码, 提取信息的程序 类似 :file 命令 rewrite 2017-09-17 爬虫俱乐部 3

1.0 版 :infix 版 1.1 版 : 修正了部分 bug, 如运行过程中临时文件 read-only 的问题 2.0 版 :mata 版 2017-09-17 爬虫俱乐部 4

subinfile filesource, [options] options: 1 index(string) specifies the line which contains it will be kept. Those lines without the key string specified by index() option will be dropped. 2 indexregex specifies that the contents you specify in index() is to be interpreted as a regular expression. 3 from(string) and to(string) specifies the string which is to be replaced whereas the to() option specifies the new string which will be used to replace the old one. 4 fromregex specifies that the contents you specify in from() is to be interpreted as a regular expression. 5 dropempty drops the empty line. If you specify both from() and dropempty, Stata will first replace the string you specify and then drop the empty line. 6 save(string) specifies the path and the file name to be saved. If you do not sepcify the format of the file, it will be saved as.txt by default. 7 replace permits save to overwrite an existing file which is not read-only. If you do not specify the option save(string), the original file will be replaced. If you sepcify the option index(string), from(string) and dropempty in one command at the same time, the option index(string) will be executed first, then from(string), and dropempty at last. 2017-09-17 爬虫俱乐部 5

1 获取页面链接 2 获取网页源代码 :copy curl 3 读入源代码 :infix import delimited fileread() 4 处理源代码 : subinfile 1 保留信息所在行 2 提取所需信息或删除多余内容 2017-09-17 爬虫俱乐部 6

1 新浪财经高管任职数据 : http://vip.stock.finance.sina.com.cn/corp/go.php/vci_corpmanager/stockid/600900.phtml 2 Statalist: https://www.statalist.org/forums/forum/general-stata-discussion/general 3 Stata 命令 : https://ideas.repec.org/s/boc/bocode.html 4 NBER 论文信息 : http://www.nber.org/papers/w20001 2017-09-17 爬虫俱乐部 7

新浪财经高管任职数据抓取 ( 单网页 ): 1 获取网页链接: 从多个链接中寻找规律, 部分网页抓取需要两次爬虫, 第一次抓取网页链接 ( 深交所年报 中国土地市场网 ) 新浪财经高管任职数据 : 长江电力 : http://vip.stock.finance.sina.com.cn/corp/go.php/vci_corpmanager/stockid/600900.phtml 万科 A: http://vip.stock.finance.sina.com.cn/corp/go.php/vci_corpmanager/stockid/000002.phtml 东信和平 : http://vip.stock.finance.sina.com.cn/corp/go.php/vci_corpmanager/stockid/002017.phtml 2017-09-17 爬虫俱乐部 8

2 可行性分析 : 能否从源代码中找到所需要提取的信息 2017-09-17 爬虫俱乐部 9

3 获取网页源代码 copy 命令 curl copy "http://vip.stock.finance.sina.com.cn/corp/go.php/vci_corpmanager/stockid/600900.phtml " temp.txt, replace 2017-09-17 爬虫俱乐部 10

4 读入源代码 (1)gb2312 读入 Stata14 要先进行转码 : unicode encoding set gb18030 unicode translate temp.txt, transutf8 // 文件前不能跟路径, 必须在工作路径下 unicode erasebackups, badidea // 固定用法, 删除备份文件 (2) 使用 infix 或者 import delimited 命令读入源代码 : infix strl v 1-100000 using temp.txt, clear import delimited using temp.txt, clear delimiters("asgdhjbaiucbiuabconobwivquviqcboqn", asstring) encoding("utf-8") 注 : 源代码只有一行情况下直接用 fileread() 函数读入 ; 源代码最后一行有所需信息时需要先先使用 file 命令加上一个回车符 : tempname temp file open `temp' using temp.txt, write text append file write `temp' _n file close `temp' 2017-09-17 爬虫俱乐部 11

5 保留需要提取的信息所在的行 : keep if index(v, "</div></td>") drop if index(v, "</strong>") v == "</div></td>" 2017-09-17 爬虫俱乐部 12

6 删除标签 ( 尖括号内的字符 ), 提取所需信息 : replace v = ustrregexra(v, "<.*?>", "") 2017-09-17 爬虫俱乐部 13

7 将提取到的信息进行整理: (1)post 命令 (2) 也可以用如下命令 : forvalues j = 1/3 { gen v`j' = v[_n + `j'] } keep if mod(_n, 4) == 1 rename (v - v3) ( 姓名职务起始日期终止日期 ) 2017-09-17 爬虫俱乐部 14

2017-09-17 爬虫俱乐部 15

2017-09-17 爬虫俱乐部 16

2017-09-17 爬虫俱乐部 17

NBER 论文信息抓取 ( 单个网页 ): 1 获取网页链接 : http://www.nber.org/papers/w20001 http://www.nber.org/papers/w20002 http://www.nber.org/papers/w20003 每个网页只有最后的编号不同 2017-09-17 爬虫俱乐部 18

例四 NBER 论文信息抓取 ( 单个网页 ): 2 可行性分析 : 抓取题目 作者 编号 时间 摘要 链接, 都出现在源代码中 2017-09-17 爬虫俱乐部 19

3 获取网页源代码 : copy "http://www.nber.org/papers/w20001" temp.txt, replace 4 读入网页源代码 : infix strl v 1-100000 using "temp.txt", clear 2017-09-17 爬虫俱乐部 20

5 保留提取信息所在的行 : keep if ustrregexm(v, `"(</h1>) (</b><br>) (</small></a></p>)"') /// index(v[_n - 1], "</h1>") /// index(v[_n - 1], `"<p style="margin-left: 40px; margin-right: 40px; text-align: justify">"') 2017-09-17 爬虫俱乐部 21

6 删除标签 ( 尖括号内的字符 ), 提取所需信息 : replace v = ustrregexra(v, `".+href="(.*?\.pdf)".+"', "$1") replace v = ustrregexra(v, "<.*?>", "") 2017-09-17 爬虫俱乐部 22

7 整理抓取的信息 : sxpose, clear rename (_var1 - _var6) (Title Author NBER_No IssuedTime Abstract URL) 2017-09-17 爬虫俱乐部 23

1 利器 主要体现在处理源代码中每行一条所需信息时 2 保留所需信息特征不在该行时无法处理 2017-09-17 爬虫俱乐部 24

1 from() 选项中的文本替换成不同的内容, 多个 from 对应多个 to( 新版功能 ) 2 正则表达式的扩充 (pcre) 3 删除掉对应的行 ( 新版功能 ) 2017-09-17 爬虫俱乐部 25