怎么用 火车头/python 中的正则表达式采集网页中的数据

我想抓取以下网页中开头部分的
5,165,228,000 和 July 21, 2017 这两个数据,如截图所示,
https://www.sec.gov/Archives/...

clipboard.png

已经试过在火车头采集器里面写了正则匹配,但是抓不出来,
<body(?<content>d{1,}(,d{3})+ )outstanding匹配5,165,228,000

as of(?<content>w{3,}&.{1,7};d{1,},s?d{4})name="匹配July 21, 2017
求教原因,或者python抓取的方法,多谢~

阅读 4.6k
3 个回答

用正则或者ocr,xpath估计也行,自己试试吧,为什么非要用火车头?

哈哈哈,用split空格

用 requests 和 BeautifulSoup 还是很简单的。

In [1]: import requests
   ...: from bs4 import BeautifulSoup
   ...: # 得到 response
   ...: resp = requests.get("https://www.sec.gov/Archives/edgar/data/320193/000032019317000009/a10-qq32017712017.htm")
   ...: soup = BeautifulSoup(resp.text, "lxml")

   ...: # 在 html 中寻找名为 text 的节点,找到后再在 text 节点中寻找名为 font 且内容为 " shares of common stock, par value " 的节点,再取得 font 标签的父节点
   ...: sentence_tag = soup.find("text").find("font", text=" shares of common stock, par value ").parent
In [2]: # 得到的节点就是要找的这句话,使用 replace 替换 latin1 字符集中的 non-breaking space \xa0 为正常空格。
   ...: sentence_tag.text.replace("\xa0", " ")
Out[2]: '5,165,228,000 shares of common stock, par value $0.00001 per share, issued and outstanding as of July 21, 2017 '
In [3]: #第一个要找的数据为 sentence_tag 的第一个子节点
   ...: list(sentence_tag)[0].text
Out[3]: '5,165,228,000'
In [4]: #第二个要找的数据为 sentence_tag 的倒数第二个子节点
   ...: list(sentence_tag)[-2].text.replace("\xa0", " ")
Out[4]: 'July 21, 2017'
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题