I'm totally new to python. I have this code:
import requests
won = 'https://api.pipedrive.com/v1/deals?status=won&start=0&api_token=xxxx'
json_data = requests.get(won).json()
deal_name = json_data ['data'][0]['title']
print(deal_name)
It prints the first title for me, but I would like it to loop through all titles in the json. But I can't figure out how. Can anyone guide me in the right direction?
You want to read up on dictionaries and lists. It seems like your json_data["data"] contains a list, so:
Seeing you wrote this:
deal_name = json_data ['data'][0]['title']
print(deal_name)
What you are looking for is:
for i in range(len(json_data["data"])):
print(json_data["data"][i]["title"])
Print it with a for loop
1. for item in json_data['data']: will take each element in the list json_data['data']
2. Then we print the title property of the object using the line print(item['title'])
Code:
import requests
won = 'https://api.pipedrive.com/v1/deals?status=won&start=0&api_token=xxxx'
json_data = requests.get(won).json()
for item in json_data['data']:
print(item['title'])
If you are ok with printing the titles as a list you can use List Comprehensions, Please refer the link in references to learn more.
print([x['title'] for x in json_data['data']])
References:
Python Loops
Python Lists
Python Comprehensions
Related
I'm getting error when I try to scrape a flashscore match summary. Example:
flashscore
I want to get the for example all the results in those page but doing driver.find_element_by_class("h2h__result") it only takes the first result. (putted inside a for obv)
If i try to do driver.find_elements_by_class i get error and i can't understand why.
Code example:
driver.get("https://www.flashscore.com/match/Qs85KCdA/#h2h/overall")
time.sleep(2)
h2h = driver.find_elements_by_class_name("rows")
for x in h2h:
p = driver.find_element_by_css_selector("span.h2h__regularTimeResult")
print(p.text)
Can someone help me to understand where i'm doing wrong? Thank you a lot guys.
The elements with class-name rows is highlighting the whole table. Use the class-name h2h__row so that all the rows are focused and will be able to extract the details from that particular row.
Try below xpaths to get the elements.
from selenium.webdriver.common.by import By
driver.get("https://www.flashscore.com/match/Qs85KCdA/#h2h/overall")
rows = driver.find_elements(By.XPATH,"//div[#class='h2h__row']")
for row in rows:
results = row.find_element(By.XPATH,".//span[#class='h2h__regularTimeResult']") # Use a dot in the xpath to find elements with in an element
print(results.text)
You can also use below CSS_SELECTOR to get the elements directly.
regularTimeResult = driver.find_elements(By.CSS_SELECTOR,"div.h2h__row span.h2h__regularTimeResult")
for item in regularTimeResult:
print(item.text)
Update:
rows = driver.find_elements(By.XPATH,"//div[#class='h2h__row']")
for row in rows:
results = row.find_element(By.XPATH,".//span[#class='h2h__regularTimeResult']") # Use a dot in the xpath to find elements with in an element
if "0 : 0" not in results.text:
print(results.text)
I have a below mentioned list:
a= ['1234,5678\n','90123,45678\n']
The expected output I'm working towards is this:
op = [['1234','5678'],['90123','45678']]
Basically a list of lists with individual elements referring to a particular column.
Using the below mentioned code i get the following output:
a = ['1234,5678\n','90123,45678\n']
new_list = []
for element in a:
#remove new lines
new_list.append(element.splitlines())
print(new_list)
output:[['1234,5678'], ['90123,45678']]
Any direction regarding this would be much appreciated.
Check this:
a= ['1234,5678\n','90123,45678\n']
a = ['1234,5678\n','90123,45678\n']
new_list = []
for element in a:
#remove new lines
new_list.append(element.strip("\n").split(","))
print(new_list)
Try this:
a = [i.strip("\n").split(",") for i in a]
Since the strings in your input list appears to follow the CSV format, you can use csv.reader to parse them:
import csv
list(csv.reader(a))
This returns:
[['1234', '5678'], ['90123', '45678']]
I am trying to build a simple random item generator for a game I am working on.
So far I am stuck trying to figure out how to store and access all of the data. I went with pandas using .csv files to store the data sets.
I want to add weighted probabilities to what items are generated so I tried to read the csv files and compile each list into a new set.
I got the program to pick a random set but got stuck when trying to pull a random row from that set.
I am getting an error when I use .sample() to pull the item row which makes me think I don't understand how pandas works. I think I need to be creating new lists so I can later index and access the various statistics of the items once one is selected.
Once I pull the item I was intending on adding effects that would change the damage and armor and such displayed. So I was thinking of having the new item be its own list then use damage = item[2] + 3 or whatever I need
error is: AttributeError: 'list' object has no attribute 'sample'
Can anyone help with this problem? Maybe there is a better way to set up the data?
here is my code so far:
import pandas as pd
import random
df = [pd.read_csv('weapons.csv'), pd.read_csv('armor.csv'), pd.read_csv('aether_infused.csv')]
def get_item():
item_class = [random.choices(df, weights=(45,40,15), k=1)] #this part seemed to work. When I printed item_class it printed one of the entire lists at the correct odds
item = item_class.sample()
print (item) #to see if the program is working
get_item()
I think you are getting slightly confused with lists vs list elements. This should work. I stubbed your dfs with simple ones
import pandas as pd
import random
# Actual data. Comment it out if you do not have the csv files
df = [pd.read_csv('weapons.csv'), pd.read_csv('armor.csv'), pd.read_csv('aether_infused.csv')]
# My stubs -- uncomment and use this instead of the line above if you want to run this specific example
# df = [pd.DataFrame({'weapons' : ['w1','w2']}), pd.DataFrame({'armor' : ['a1','a2', 'a3']}), pd.DataFrame({'aether' : ['e1','e2', 'e3', 'e4']})]
def get_item():
# I removed [] from the line below -- choices() already returns a list of length 1
item_class = random.choices(df, weights=(45,40,15), k=1)
# I added [0] to choose the first element of item_class which is a list of length 1 from the line above
item = item_class[0].sample()
print (item) #to see if the program is working
get_item()
prints random rows from random dataframes that I setup such as
weapons
1 w2
I have a large set of data which is being parsed by feedparser and is enumerated as a string. For instance:
import feedparser
d = feedparser.parse('somefeed.xml')
will give elements such as d.entries['title'], d.entries['url'], and other d.entries which are strings.
I am wanting to compare these elements against a list I have defined to see if there is a match and I am not quite thinking something through correctly.
Below is what I tried, but got no output, any help is appreciated.
for i in d.entries:
my_list = ["Title One", "Title Two", "Etc"]
if my_list in i['title'].split("-"):
print(i)
If there is a match of parsed data and an element of my list, I want to print that element.
elem = browser.find_element_by_partial_link_text("WEBSITE")
above code finds out element with a link text as WEBSITE, but I don't want to use Selenuim here and Find element by text by using bs4. I tried the following code but no results
elem = soup.find(text=re.compile('WEBSITE'))
By the documentation provided here. You can do something like below.
ele = soup.find('tag', string='text_for_search')