如何在Python3中使用gspread在相应URL旁边的左侧单元格中打印xpath的值?

问题描述 投票:1回答:1

我的目的是从Google Sheet第四列中的一系列URL中删除xpath的值,并在URL左侧的单元格中打印该值。

到目前为止我有以下内容,但是当我运行它时,它会打印所有URL的adGroupStatus列表的最后一个值,而不是每个相应URL的正确值。

有人能提供解决方案吗?

import requests
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from lxml import html

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

sh = client.open('example_sheet_name')
worksheet = sh.get_worksheet(0)

# the column (4th) with our URLs
url_list = worksheet.col_values(4)
# where we want our xpath values to print to
cell_list = worksheet.range('C1:C5')

def grab_xpathtext(urls, cell_range):
    # do the below for each url in the spreadsheet column 4:
    for url in urls:
        r = requests.get(url)
        tree = html.fromstring(r.content)
        adGroupStatus = tree.xpath('//*[@id="m_count"]/text()')
                # below prints each value to the cmd line on a new line as expected
        print(adGroupStatus[0])
    for cell in cell_range:
        # below prints the last value instead of each corresponding value
        cell.value = adGroupStatus[0]
    worksheet.update_cells(cell_range)

grab_xpathtext(url_list, cell_list)

我希望输出类似于:

|位置1 |描述| 1 |网址1 |

|位置2 |描述| 2 |网址2 |

|位置3 |描述| 3 |网址3 |

|位置4 |描述| 4 |网址4 |

|位置5 |描述| 5 |网址5 |

......但我得到了这个:

|位置1 |描述| 5 |网址1 |

|位置2 |描述| 5 |网址2 |

|位置3 |描述| 5 |网址3 |

|位置4 |描述| 5 |网址4 |

|位置5 |描述| 5 |网址5 |

python-3.x gspread
1个回答
0
投票

我在另一个问题中找到了答案:Python/gspread - how can I update multiple cells with DIFFERENT VALUES at once?

实施为:

url_list = worksheet.col_values(4)
cell_list = worksheet.range('C1:C5')

def grab_xpathtext(urls, cell_range):
        statuses = []
        for url in urls:
            r = requests.get(url)
            tree = html.fromstring(r.content)
            adGroupStatus = tree.xpath('//*[@id="m_count"]/text()')
            statuses.append(adGroupStatus[0])
        print(statuses)
        for cell in cell_range:
            for i, val in enumerate(statuses):
                cell_range[i].value = val
        worksheet.update_cells(cell_range)

grab_xpathtext(url_list, cell_list)
© www.soinside.com 2019 - 2024. All rights reserved.