Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14155

Can I make conditional statements with HTML tags in python?

$
0
0

I am doing a web scraping project. My data is from Wikipedia, List of largest companies in the United States by revenue. When I extract the data (the first table), I can't get the revenue growth column to reflect if it was positive or negative because it is depicted by green triangles pointing upwards and red triangles pointing downwards. Below is my code:

from bs4 import BeautifulSoup  import requestsurl = 'https://en.wikipedia.org/wiki/List_of_largest_companies_in_the_United_States_by_revenue'source = requests.get(url)  soup = BeautifulSoup(source.text, 'html')table = soup.find_all('table', class_='wikitable sortable')[0]table_data = table.find_all('tr')[1:]table_df = []  for row in table_data:      row_data = row.find_all('td')      row_df = [data.text.strip() for data in row_data]    table_df.append(row_df)table_df

How can I get revenue growth to show + or -?

table_df = []  for row in table_data:      row_data = row.find_all('td')    row_df = [data.text.strip() for data in row_data]    if row_data.find_all('img', alt='Decrease') == True:        row_df[4] = '-'+ row_df[4]    table_df.append(row_df)table_df

Tried this code and it resulted in the same output as the original code


Viewing all articles
Browse latest Browse all 14155

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>