簡體   English   中英

我的第一個Python Web Scraper的問題

[英]Issues with my first Python Web Scraper

我正在編寫我的第一個python Web爬蟲,但在編寫代碼以獲取它來爬取所需數據時遇到了麻煩。

到目前為止,這是我的代碼:

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但它只是返回頁面的整個源代碼。

您可以通過將href值與CSS選擇器 部分匹配來找到所需的鏈接:

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

出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM