我的第一个Python Web Scraper的问题

时间:2017-01-08 02:29:15

标签: python web-scraping

我正在编写我的第一个python web scraper,我在编写代码时遇到了麻烦,无法抓取我想要的数据。

到目前为止,这是我的代码:

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)   

我想从网站上提取的是这部分代码:

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

这是整行,但我只对以上部分感兴趣:

<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>

具体来说,我想拉动股票代码,在这种情况下是$ ETRM。我想从上面的页面中提取上述格式的所有股票代码。

我尝试隔离quote.ashx?t,但它只是返回页面的完整源代码。

2 个答案:

答案 0 :(得分:0)

您可以通过部分匹配 href值与CSS selector找到所需的链接:

link = soup.select_one("a[href*=ETRM]")
print(link["href"])

答案 1 :(得分:0)

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

出:

[<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>]