python的web抓取_Python高效的Web抓取?

I'm essentially using urllib to open the desired webpage for each

stock in the argument list and reading the full contents of the html

code for that page. Then I'm slicing that in order to find the quote

I'm looking for.

以下是^{}和requests中的实现:import requests

from bs4 import BeautifulSoup

def get_quotes(*stocks):

quotelist = {}

base = 'https://finance.google.com/finance?q={}'

for stock in stocks:

url = base.format(stock)

soup = BeautifulSoup(requests.get(url).text, 'html.parser')

quote = soup.find('span', attrs={'class' : 'pr'}).get_text().strip()

quotelist[stock] = float(quote)

return quotelist

print(get_quotes('AAPL', 'GE', 'C'))

{'AAPL': 160.86, 'GE': 23.91, 'C': 68.79}

# 1 loop, best of 3: 1.31 s per loop

如评论中所述,您可能需要查看multithreading或{a3}。在

使用grequests发出异步HTTP请求:

^{pr2}$

更新:这是Dusty Phillips的python3面向对象编程的一个修改版本,它使用了内置的threading模块。在from threading import Thread

from bs4 import BeautifulSoup

import numpy as np

import requests

class QuoteGetter(Thread):

def __init__(self, ticker):

super().__init__()

self.ticker = ticker

def run(self):

base = 'https://finance.google.com/finance?q={}'

response = requests.get(base.format(self.ticker))

soup = BeautifulSoup(response.text, 'html.parser')

try:

self.quote = float(soup.find('span', attrs={'class':'pr'})

.get_text()

.strip()

.replace(',', ''))

except AttributeError:

self.quote = np.nan

def get_quotes(tickers):

threads = [QuoteGetter(t) for t in tickers]

for thread in threads:

thread.start()

for thread in threads:

thread.join()

quotes = dict(zip(tickers, [thread.quote for thread in threads]))

return quotes

tickers = [

'A', 'AAL', 'AAP', 'AAPL', 'ABBV', 'ABC', 'ABT', 'ACN', 'ADBE', 'ADI',

'ADM', 'ADP', 'ADS', 'ADSK', 'AEE', 'AEP', 'AES', 'AET', 'AFL', 'AGN',

'AIG', 'AIV', 'AIZ', 'AJG', 'AKAM', 'ALB', 'ALGN', 'ALK', 'ALL', 'ALLE',

]

%time get_quotes(tickers)

# Wall time: 1.53 s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值