pandas最方便 |
求大神帮忙看一下 import time from selenium import webdriver from bs4 import BeautifulSoup from selenium.webdriver.common.action_chains import ActionChains def search(keys): driver=webdriver.Chrome() driver.maximize_window() #打开google,搜索关键词,并点击搜索结果第1个 try: driver.get('https://www.google.com/search?q='+ keys+'&rlz=1C1CHBF_enUS886US886&oq='+keys+'&aqs=chrome.0.69i59j69i60.4967j0j15&sourceid=chrome&ie=UTF-8') time.sleep(2) driver.find_element_by_css_selector('div>h3>a').get_attribute("href") #打印搜索结果的尺寸,文本,并进入第一条记录 except: tem=' ' else: tem = driver.find_element_by_css_selector('div>h3>a').get_attribute("href") driver.close() return tem import pandas as pd url = [] df = pd.read_excel('实验2.xlsx',index_col=0) for i in range(len(df)): keys = df.iloc[i,0]+' '+df.iloc[i,1] url.append(search(keys)) df['website'] = url df.to_excel('final123.xlsx') 最后excel里面只有website,但是这一列下面没有数据 |