2017-06-02 95 views
0

我想刮掉谷歌財經上列出的公司名稱,網址和描述。到目前爲止,我成功地獲取了描述和url,但無法獲取名稱。在myUrl的源代碼中,name是024 Pharma Inc.。當我看到div時,這個類被命名爲'appbar-snippet-primary'。但是代碼仍然沒有找到它。我馬上新到網頁抓取,所以可能是我失去了一些東西。請在這方面指導我。無法從谷歌金融刮名稱

from bs4 import BeautifulSoup 
import urllib 
import csv 

myUrl = 'https://www.google.com/finance?q=OTCMKTS%3AEEIG' 

r = urllib.urlopen(myUrl).read() 
soup = BeautifulSoup(r, 'html.parser') 

name_box = soup.find('div', class_='appbar-snippet-primary') # !! This div is not found 
#name = name_box.text 
#print name 

description = soup.find('div', class_='companySummary') 
desc = description.text.strip() 
#print desc 

website = soup.find('div', class_='item') 
site = website.text 
#print site 
+0

https://stackoverflow.com/questions/5913280/beautifulsoup-and-ajax-table-problem並且還HTTPS ://pypi.python.org/pypi/googlefinance –

+0

由於這個div是在javascript中動態生成的,因此您無法找到div'appbar-snippet-primary',您需要'selenium'或'splash'來取消這種網頁。 –

回答

0
from bs4 import BeautifulSoup 
import requests 

myUrl = 'https://www.google.com/finance?q=OTCMKTS%3AEEIG' 

r = requests.get(myUrl).content 
soup = BeautifulSoup(r, 'html.parser') 

name = soup.find('title').text.split(':')[0] # !! This div is not found 
#print name 

description = soup.find('div', class_='companySummary') 
desc = description.text.strip() 
#print desc 

website = soup.find('div', class_='item') 
site = website.text 
-1

寫入soup.find_all()而不是soup.find()