给定抓取的网站文本,无法将正则表达式获取到 return
Trouble getting regular expression to return given scraped website's text
所以我抓取了一个网站,现在我正在尝试使用 RE 提取文本的某些部分。
source = urllib.request.urlopen('http://www.nasdaq.com/article/2017-small-cap-biotech-watchlist-takeaways-for-investors-cm739688')
soup = bs.BeautifulSoup(source,'html.parser')
for paragraph in soup.find_all('p'):
print(paragraph.text)
产生如下内容:
In a panel discussion moderated by Maxim Group's senior managing director and the head of healthcare research Jason Kolbert, Raghuram "Ram" Selvaraju of Rodman & Renshaw; George Zavoico, senior equity analyst at JonesTrading Institutional Services; and Mike King, managing director and senior biotechnology analyst at JMP Securities, detailed their investment theses for the companies that made the cut.
King chose nine companies for the list, including OncoMed Pharmaceuticals Inc. (OMED:NASDAQ) , a company "we've had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).
King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren't expected until 2019, but King is "optimistic about Syndax's chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."
Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we'll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."
我正在尝试提取如下所示的股票代码:
(OMED:NASDAQ)
(SYRS:NASDAQ)
我正在尝试
pattern = re.compile(r':\(\w+\)')
for paragraph in soup.find_all('p'):
print(paragraph.find_all(text =pattern))
但它正在屈服
[]
[]
[]
[]
[]
编辑:
还有,试过了
pattern = re.compile(r'\(\w+:\w+\)' )
和`pattern = re.compile(r'([A-Z]+:[A-Z]+)')'
两者都产生了这样的结果:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
['OncoMed Pharmaceuticals Inc. (OMED:NASDAQ)', ' , a company "we\'ve had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).']
['King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren\'t expected until 2019, but King is "optimistic about Syndax\'s chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."']
['Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we\'ll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."']
尝试:
pattern = re.compile(r'\(\w+:\w+\)' )
# This will match any words in range of [0-9a-zA-Z]
示例位于:
https://regex101.com/r/P2yqzF/3
也许re.findall(pattern, paragraph.text)
可以解决你的第二个问题
尝试
pattern = re.compile(r'\([A-Z]+:[A-Z]+\)')
寻找 <open parantheses><caps letter><colon><caps letter><close parantheses>
然后,为了找到字符串,你可以re.findall(paragraph)
(假设段落是一个字符串)
paragraphs = '''\
In a panel discussion moderated by Maxim Group's senior managing director and the head of healthcare research Jason Kolbert, Raghuram "Ram" Selvaraju of Rodman & Renshaw; George Zavoico, senior equity analyst at JonesTrading Institutional Services; and Mike King, managing director and senior biotechnology analyst at JMP Securities, detailed their investment theses for the companies that made the cut.
King chose nine companies for the list, including OncoMed Pharmaceuticals Inc. (OMED:NASDAQ) , a company "we've had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).
King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren't expected until 2019, but King is "optimistic about Syndax's chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."
Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we'll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."'''
import re
exp = re.compile(r'(\([A-Z]+\:[A-Z]+\))')
for para in paragraphs.split('\n'):
print (para)
print (exp.findall(para))
结果:
In a panel discussion moderated by Maxim Group's senior managing director and the head of healthcare research Jason Kolbert, Raghuram "Ram" Selvaraju of Rodman & Renshaw; George Zavoico, senior equity analyst at JonesTrading Institutional Services; and Mike King, managing director and senior biotechnology analyst at JMP Securities, detailed their investment theses for the companies that made the cut.
[]
King chose nine companies for the list, including OncoMed Pharmaceuticals Inc. (OMED:NASDAQ) , a company "we've had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).
['(OMED:NASDAQ)', '(CELG:NASDAQ)']
King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren't expected until 2019, but King is "optimistic about Syndax's chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."
['(SNDX:NASDAQ)']
Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we'll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."
['(SYRS:NASDAQ)']
所以我抓取了一个网站,现在我正在尝试使用 RE 提取文本的某些部分。
source = urllib.request.urlopen('http://www.nasdaq.com/article/2017-small-cap-biotech-watchlist-takeaways-for-investors-cm739688')
soup = bs.BeautifulSoup(source,'html.parser')
for paragraph in soup.find_all('p'):
print(paragraph.text)
产生如下内容:
In a panel discussion moderated by Maxim Group's senior managing director and the head of healthcare research Jason Kolbert, Raghuram "Ram" Selvaraju of Rodman & Renshaw; George Zavoico, senior equity analyst at JonesTrading Institutional Services; and Mike King, managing director and senior biotechnology analyst at JMP Securities, detailed their investment theses for the companies that made the cut.
King chose nine companies for the list, including OncoMed Pharmaceuticals Inc. (OMED:NASDAQ) , a company "we've had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).
King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren't expected until 2019, but King is "optimistic about Syndax's chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."
Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we'll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."
我正在尝试提取如下所示的股票代码:
(OMED:NASDAQ)
(SYRS:NASDAQ)
我正在尝试
pattern = re.compile(r':\(\w+\)')
for paragraph in soup.find_all('p'):
print(paragraph.find_all(text =pattern))
但它正在屈服
[]
[]
[]
[]
[]
编辑:
还有,试过了
pattern = re.compile(r'\(\w+:\w+\)' )
和`pattern = re.compile(r'([A-Z]+:[A-Z]+)')'
两者都产生了这样的结果:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
['OncoMed Pharmaceuticals Inc. (OMED:NASDAQ)', ' , a company "we\'ve had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).']
['King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren\'t expected until 2019, but King is "optimistic about Syndax\'s chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."']
['Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we\'ll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."']
尝试:
pattern = re.compile(r'\(\w+:\w+\)' )
# This will match any words in range of [0-9a-zA-Z]
示例位于: https://regex101.com/r/P2yqzF/3
也许re.findall(pattern, paragraph.text)
可以解决你的第二个问题
尝试
pattern = re.compile(r'\([A-Z]+:[A-Z]+\)')
寻找 <open parantheses><caps letter><colon><caps letter><close parantheses>
然后,为了找到字符串,你可以re.findall(paragraph)
(假设段落是一个字符串)
paragraphs = '''\
In a panel discussion moderated by Maxim Group's senior managing director and the head of healthcare research Jason Kolbert, Raghuram "Ram" Selvaraju of Rodman & Renshaw; George Zavoico, senior equity analyst at JonesTrading Institutional Services; and Mike King, managing director and senior biotechnology analyst at JMP Securities, detailed their investment theses for the companies that made the cut.
King chose nine companies for the list, including OncoMed Pharmaceuticals Inc. (OMED:NASDAQ) , a company "we've had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).
King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren't expected until 2019, but King is "optimistic about Syndax's chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."
Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we'll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."'''
import re
exp = re.compile(r'(\([A-Z]+\:[A-Z]+\))')
for para in paragraphs.split('\n'):
print (para)
print (exp.findall(para))
结果:
In a panel discussion moderated by Maxim Group's senior managing director and the head of healthcare research Jason Kolbert, Raghuram "Ram" Selvaraju of Rodman & Renshaw; George Zavoico, senior equity analyst at JonesTrading Institutional Services; and Mike King, managing director and senior biotechnology analyst at JMP Securities, detailed their investment theses for the companies that made the cut.
[]
King chose nine companies for the list, including OncoMed Pharmaceuticals Inc. (OMED:NASDAQ) , a company "we've had a long-term passion for." He considers OncoMed a "leader in the cancer stem cell space," with a number of clinical assets including a handful that are proprietary, and many partnered with larger-cap pharmas and biotechs. Among the candidates slated for either clinical trials or data readouts in 2017 is demcizumab, in pancreatic cancer. "We think it will be a key catalyst for the shares," King said. Other assets include tarextumab, for a small cell lung cancer; DLL4/VEG4, also in non-small cell lung cancer; and "a new asset that was previously known as I/O#2. . .a very highly validated immuno-oncology target called TIGIT," in development in partnership with Celgene Corp. (CELG:NASDAQ).
['(OMED:NASDAQ)', '(CELG:NASDAQ)']
King called Syndax Pharmaceuticals Inc. (SNDX:NASDAQ) "a hybrid story. " The company is a player in the immuno-oncology space as well as in the "broader" cancer world with a drug called entinostat, which is in Phase 3 in metastatic breast cancer. Readouts on overall survival aren't expected until 2019, but King is "optimistic about Syndax's chances for success in the Phase 3 trial." The company is also in collaborations with major pharmas in the "immune checkpoint space," with data expected to "roll out over the course of 2017."
['(SNDX:NASDAQ)']
Syros Pharmaceuticals (SYRS:NASDAQ) is an early-stage company, with a ~0M market cap, that King considers a leader in gene regulation, with a concept called super-enhancers. The company both develops its "own internal candidates to some of the highly validated targets," as well as repurposes assets that may have been abandoned for indications like blood disorders. Syros might be "a quiet story the first half of 2017, but I think in the second half of 2017, we'll start to see some fireworks based on the data that will read out through key assets, SY-1365 and SY-1425."
['(SYRS:NASDAQ)']