将for循环输出结果写入CSV文件

时间:2020-01-04 21:41:05

标签: csv for-loop output google-custom-search

尽管在线阅读量很大,但长时间潜伏在这个问题上还是感到困惑。我建立了一个“系列”布尔搜索词,希望将其自动搜索到我的Google自定义搜索引擎中。随后,我希望获取网页的标题和从每个搜索词生成的URL,并将此信息存储为.csv文件。

目前在下面,我能够将每个连续搜索词的标题和URL打印到控制台中,但是我不确定如何继续将其保存到.csv文件中。

!pip install google-api-python-client

api_key = "Omitted on purpose"

from apiclient.discovery import build

resource = build("customsearch", 'v1', developerKey=api_key).cse()

# Plugging Google Search Terms into Custom Search Engine
for searchTerm in df4:
    result = resource.list(q=searchTerm, cx='omitted on purpose').execute()
    for item in result['items']:            
        print(item['title'],item['link'])

下面是输出示例:

Dr. Lowell D Mason II MD Reviews | Foley, AL | Vitals.com https://www.vitals.com/doctors/Dr_Lowell_Mason.html
Dr. Lowell Mason II, MD - Reviews - Mobile, AL https://www.healthgrades.com/physician/dr-lowell-mason-ywykj
Dr. Lowell Mason II, MD – Mobile, AL | Orthopaedic Surgery https://www.doximity.com/pub/lowell-mason-md

0 个答案:

没有答案