简体   繁体   中英

Issues with my first Python Web Scraper

I'm writing my first python web scraper and I'm having trouble writing the code to get it to scrape the data I want.

Here is my code so far:

import bs4 as bs
import urllib.request

source = urllib.request.urlopen ('http://finviz.com/screener.ashx?v=340&s=ta_topgainers')
soup = bs.BeautifulSoup(source, "html.parser")
#Ticker = 'quote.ashx?t'

print (Ticker)   

What I want to pull from the website is this section of code:

<a href="quote.ashx?t=ETRM&ty=c&p=d&b=1">

This is the entire line but I'm only interested in the section above:

<a href="quote.ashx?t=ETRM&ty=c&p=d&b=1"><img src="chart.ashx?t=ETRM&ta=1&ty=c&p=d&s=l" alt="" width="700" height="340" border="0"/></a></td>

Specifically I want to pull the ticker symbol, which in this case is $ETRM. I would like to pull all the ticker symbols from the page above that are in the format above.

I tried isolating the quote.ashx?t but it's just returning the entire source code of the page.

You can locate the desired link by partially matching the href value with a CSS selector :

link = soup.select_one("a[href*=ETRM]")
print(link["href"])
soup.select('a[href^="quote.ashx?t"]') # select a tag which have href starts with quote.ashx?t

out:

[<a href="quote.ashx?t=ETRM&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=ETRM&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=ETRM&amp;ty=c&amp;p=d&amp;b=1">ETRM</a>,
 <a href="quote.ashx?t=SSY&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=SSY&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=SSY&amp;ty=c&amp;p=d&amp;b=1">SSY</a>,
 <a href="quote.ashx?t=PTX&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=PTX&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=PTX&amp;ty=c&amp;p=d&amp;b=1">PTX</a>,
 <a href="quote.ashx?t=ZFGN&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=ZFGN&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=ZFGN&amp;ty=c&amp;p=d&amp;b=1">ZFGN</a>,
 <a href="quote.ashx?t=JTPY&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=JTPY&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=JTPY&amp;ty=c&amp;p=d&amp;b=1">JTPY</a>,
 <a href="quote.ashx?t=ARWR&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=ARWR&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=ARWR&amp;ty=c&amp;p=d&amp;b=1">ARWR</a>,
 <a href="quote.ashx?t=PCRX&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=PCRX&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=PCRX&amp;ty=c&amp;p=d&amp;b=1">PCRX</a>,
 <a href="quote.ashx?t=ATOS&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=ATOS&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=ATOS&amp;ty=c&amp;p=d&amp;b=1">ATOS</a>,
 <a href="quote.ashx?t=QTNT&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=QTNT&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=QTNT&amp;ty=c&amp;p=d&amp;b=1">QTNT</a>,
 <a href="quote.ashx?t=GBX&amp;ty=c&amp;p=d&amp;b=1"><img alt="" border="0" height="340" src="chart.ashx?t=GBX&amp;ta=1&amp;ty=c&amp;p=d&amp;s=l" width="700"/></a>,
 <a class="tab-link" href="quote.ashx?t=GBX&amp;ty=c&amp;p=d&amp;b=1">GBX</a>]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM