извлечь заголовок из ссылки, используя BeautifulSoup - PullRequest
0 голосов
/ 09 марта 2020

Я использую Beautifulsoup, чтобы отказаться от веб-сайта, но мне нужна помощь с этим, так как я новичок в python и BeautifulSoup. Как мне получить ПОО из следующих "[[VET]]"

Это мой код пока

import bs4 as bs
import urllib.request
import pandas as pd


#This is the Home page of the website
source = urllib.request.urlopen('file:///C:/Users/Aiden/Downloads/stocks/Stock%20Premarket%20Trading%20Activity%20_%20Biggest%20Movers%20Before%20the%20Market%20Opens.html').read().decode('utf-8')

soup = bs.BeautifulSoup(source,'lxml')


#find the Div and put all info into varTable
table = soup.find('table',{"id":"decliners_tbl"}).tbody



#find all Rows in table and puts into varTableRows
tableRows = table.find_all('tr')
print ("There is ",len(tableRows),"Rows in the Table")
print(tableRows)

columns = [tableRows[1].find_all('td')]
print(columns)

a = [tableRows[1].find_all("a")]
print(a)

So my output from print(a) is "[[<a class="mplink popup_link" href="https://marketchameleon.com/Overview/VET/">VET</a>]]"
 and I want to extract VET out 

AD

Ответы [ 2 ]

0 голосов
/ 10 марта 2020

Спасибо за ответ, я смог разобраться, используя следующий код

source = urllib.request.urlopen('file:///C:/Users/Aiden/Downloads/stocks/Stock%20Premarket%20Trading%20Activity%20_%20Biggest%20Movers%20Before%20the%20Market%20Opens.html').read().decode('utf-8')


soup = bs.BeautifulSoup(source,'html.parser')

table = soup.find("table",id="decliners_tbl")

for decliners in table.find_all("tbody"):
    rows = decliners.find_all("tr")
    for row in rows:
        ticker = row.find("a").text
        volume = row.findAll("td", class_="rightcell")[3].text
        print(ticker, volume)
0 голосов
/ 09 марта 2020

Вы можете использовать a.text или a.get_text ().

Если у вас есть несколько элементов, вам потребуется понимание списка для этой функции

...