Find value of cell next to searched cell? - python

I'm trying to find the value for the cell NEXT to the cell I've found the value in. I have a column for tags and for links. If the link has a certain tag, the links value should be displayed. How can I do this in python with google-spreadsheet API?
I couldn't find a solution in the documentations.
ll = sheet.findall(topic)
for i in ll:
print(i.value)
How can I change it so it outputs the value of the column next to the cell?

Related

Trying to find HREF from table with Selenium in Python

Webscraping a table into an Excel-file. Its a "Dynamic" table per 10 rows.
All the data is placed into Excel correctly, But having issues with the HREF-data.
The issue i am facing is that some rows dont have a HREF. I am using the following Xpath:
map = driver.find_elements(By.XPATH,'//*[#id="table_1"]/tbody//td[12]/a')
To get the HREF:
.get_attribute("href")[30:].split(",%20")[0]
.get_attribute("href")[30:].split(",%20")[1]
Via above Xpath is can find every HREF, but in case of NO HREF in the row, the following HREF-data is placed into the row where NO HREF-data should be.
Tried the below (without the "/a") but it returns nothing.
map_test = driver.find_elements(By.XPATH, '//*[#id="table_1"]/tbody//td[12]')
When below code is used, it returns the text content which is not what I need, but keeps the data where is should be.
.get_attribute("textContent")
Any idea how i can find the HREFs and keep the data in the rows where it should be?

Xpath seems didn't return expected span

I am trying to use xpath to download all images from a webpage.
Have managed to found the specific element which has several spans, and the full xpath looks as below:
/html/body/div[2]/div[3]/main/ul/li[4]/article/div[1]/a/span/span[1]
/html/body/div[2]/div[3]/main/ul/li[4]/article/div[1]/a/span/span[2]
/html/body/div[2]/div[3]/main/ul/li[4]/article/div[1]/a/span/span[3]
etc.
Currently I've get the whole element till "li[4]" level and tried to use below code to find all the leaves elements of the tree, but the returned value is empty:
->node.xpath('./article/div[#class="flex-box"]/a/span[starts-with(#class,"grid-box")]/span')
->[]
And the parent node length is only 1 instead of the number of the leaves which I expected to be at least 4-5 here:
->len(node.xpath('./article/div[#class="flex-box"]/a/span[starts-with(#class,"grid-box")]'))
->1
->node.xpath('./article/div[#class="flex-box"]/a/span[starts-with(#class,"grid-box")]')[0]
-><Element span at 0x1ac51134040>
Any one could help me figure out what is going on here?

python selenium cache refreshing value before change again

In my Selenium-Python project, there is a page with some tables. the value of cell's of this tables, change every 2-3 second's.
i want to do some stuff with value's of cell's like color and text. but i get this error
Message: stale element reference: element is not attached to the page document
for example in this code before i can fetch all rows, new table comes up and error happen:
usids = browser.find_elements_by_xpath("//div[contains(#class,'usid')]")
for x in range(len(usids)):
if usids[x].get_attribute("class").find("names") != -1:
print(usids[x].text)
how can i fix it?

how to retrieve hyperlinks in spreadsheet cells using gspread?

I am not able to retrieve hyperlinks in google spreadsheet cells using gspread. I am always returned the text of the cell and not the hyperlink itself.
I have attempted
worksheet.cell(i, j, value_render_option="FORMULA")
with all the three possible options for value_render_option and none of them works.
I have seen some old answers here about using input_value, that unfortunately is not supported anymore
If your cell content is something like
=HYPERLINK("http://www.wikipedia.de","wikipedia")
try
cell = worksheet.cell(i, j, value_render_option='FORMULA').value

(HTML Scraping) XPath of a column changes based on color

I am trying to parse through all of the values in the column of this website (with different stock tickers). I am working in Python and am using XPath to scrape HTML data.
Lets say I want to extract the value of the "Change" which is currently 0.62% (and green). I would first get the tree to the website and then say.
stockInfo_1 = tree.xpath('//*[#class="table-dark-row"]/td[12]/b/span/text()')
I would then get an array of values and last element happens to be change value.
However, I noticed that if a value in this column has a color, it is in the /b/SPAN, while if it does not have a color, there is no span and its just in the /b.
So to explain:
stockInfo_1 = tree.xpath('//*[#class="table-dark-row"]/td[12]/b/span/text()')
^this array would have every value in this column that is Colored
while stockInfo_1 = tree.xpath('//*[#class="table-dark-row"]/td[12]/b/text()')
^would have every value in the column that does not have a color.
The colors are not consistent for each stock. Some stocks have random values that have colors and some do not. So that messes up the /b/span and /b array consistency.
How can I get an array of variables of ALL of the values (in order) in each column regardless of if they are in a span or not? I do not care about the colors, i just care about the values.
I can explain more if needed. Thanks!!
You can directly skip intermediate tags in xpath and get all the values in a list by using // inbetween.
So the snippet should be
tree.xpath('//*[#class="table-dark-row"]/td[12]/b//text()')
This skips all the intermediate tags between and text.
I've tried using lxml. Here is the code
import requests
from lxml import html
url="https://finviz.com/quote.ashx?t=acco&ty=c&ta=1&p=d"
resp=requests.get(url)
tree = html.fromstring(resp.content)
values = tree.xpath('//*[#class="table-dark-row"]/td[12]/b//text()')
print values
Which gives output as follows
['0.00%', '-2.43%', '-8.71%', '-8.71%', '7.59%', '-1.23%', '1.21', '0.30', '2.34% 2.38%', '12.05', '12.18', '1.04%']
Note: If you don't want to hardcode 12 in the above Xpath you can aslo use last() as tree.xpath('//*[#class="table-dark-row"]/td[last()]/b//text()')
Xpath cheat sheet for your kind reference.
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion

Categories