我正在嘗試通過(guò)使用名稱列表作為輸入并在DataFame中獲取數(shù)據(jù)集來(lái)在Google搜索上進(jìn)行網(wǎng)絡(luò)抓取。我以前使用硒進(jìn)行網(wǎng)絡(luò)抓取,我很難使用循環(huán)來(lái)構(gòu)建語(yǔ)法,以運(yùn)行名稱列表作為輸入,以獲取結(jié)果并抓取每個(gè)頁(yè)面。以下是我的Python代碼:baseUrl = 'https://www.google.com/search?q='pluseUrl = input('CEO: ') url = baseUrl + quote_plus(pluseUrl)browser = webdriver.Chrome(r"C:\Users\...\chromedriver.exe")browser.get(url)table = browser.find_elements_by_css_selector('div.ifM9O') df = pd.DataFrame(columns = ['ceo', 'value'])values =[]for row in table: ceo = str(([c.text for c in row.find_elements_by_css_selector('div.kno-ecr-pt.PZPZlf.gsmt.i8lZMc')])).strip('[]').strip("''") value = str(([c.text for c in row.find_elements_by_css_selector('div.Z1hOCe')])).strip('[]').strip("''")ceo = pd.Series(ceo)value = pd.Series(value)df = df.assign(**{'ceo': ceo, 'value': value}) print(df)以下是將比爾·蓋茨作為輸入后的結(jié)果:CEO: Bill gates ceo value0 Bill Gates Born: October 28, 1955 (age 64 years), Seattle...任何意見或建議將不勝感激。
1 回答

ABOUTYOU
TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
試試這個(gè):
baseUrl = 'https://www.google.com/search?q='
browser = webdriver.Chrome(r"C:\Users\...\chromedriver.exe")
input_list = ["Bill Gates", "Elon Musk", "Warren Buffet"]
output = {}
def scrape_ceo_list(list_of_ceo):
for ceo in list_of_ceo:
browser.get(baseUrl + ceo)
// query selectors, dataframes etc as per original code
// ...
output[ceo] = df
output現(xiàn)在是數(shù)據(jù)幀的字典,CEO 名稱作為字典鍵。
添加回答
舉報(bào)
0/150
提交
取消