Moondra
Moondra

Reputation: 4521

Trouble getting regular expression to return given scraped website's text

So I scraped a website and now I'm trying to using RE to pull out certain parts of the text.

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)

yields something like:

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 ~$250M 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."

I'm trying to pull out the stock tickers which look like this:

(OMED:NASDAQ)
(SYRS:NASDAQ)

I'm trying

pattern = re.compile(r':\(\w+\)')
for paragraph in soup.find_all('p'):
    print(paragraph.find_all(text =pattern))

but it's yielding

[]
[]
[]
[]
[]

EDIT:

Also,tried pattern = re.compile(r'\(\w+:\w+\)' ) and `pattern = re.compile(r'([A-Z]+:[A-Z]+)')'

both are yielding something like this:

[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
['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 ~$250M 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."']

Upvotes: 0

Views: 43

Answers (3)

Ludisposed
Ludisposed

Reputation: 1769

try:

pattern = re.compile(r'\(\w+:\w+\)' )
# This will match any words in range of [0-9a-zA-Z]

Example at: https://regex101.com/r/P2yqzF/3

Maybe re.findall(pattern, paragraph.text) will solve your second problem

Upvotes: 1

Bill Bell
Bill Bell

Reputation: 21663

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 ~$250M 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))

Results:

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 ~$250M 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)']

Upvotes: 1

Mohammad Athar
Mohammad Athar

Reputation: 1980

try

pattern = re.compile(r'\([A-Z]+:[A-Z]+\)')

That looks for <open parantheses><caps letter><colon><caps letter><close parantheses>

Then, in order to find the string, you can do re.findall(paragraph)

(assuming paragraph is a string)

Upvotes: 1

Related Questions