大家好,我的網路抓取幾乎完成了,我正試圖弄清楚最后一步。哪個是執行步驟;網頁抓取,保存到資料框,最后保存到excel。在一個清單上。
例如,這是代碼:
driver.get("website")
wait = WebDriverWait(driver, 20)
search_box = wait.until(EC.visibility_of_element_located((By.ID,"Search"))).send_keys("BC-9700021-1")
driver.switch_to.default_content()
submit_box = wait.until(EC.visibility_of_element_located((By.ID,"Submit"))).click()
order_list = []
order_info = {}
soup = BeautifulSoup(driver.page_source,'html.parser')
def correct_tag(tag):
return tag.name == "span" and tag.get_text(strip=True) in {
"Order Amount",
"Item Name",
"Date",
"Warehouse Number",
}
for t in soup1.find_all(correct_tag):
order_info[t.text] = t.find_next_sibling(text=True).strip()
order_list.append(order_info)
order_df1 = pd.DataFrame(order_list)
datatoexcel = pd.ExcelWriter('Order_sheet.xlsx')
order_df1.to_excel(datatoexcel)
datatoexcel.save()
輸出:
Order Amount: 7000
Item Name: Plastic Cup
Date: 7/1/2022
Warehouse Number: 000718
但是在我輸入搜索框“BC-9700021-1”的最頂部,我希望能夠從保存在 excel 中的串列中提取特定搜索。所以excel表會有一個這樣的串列:
BC-9700021-1
BC-9700024-1
BC-9700121-2
ETC.
ETC.
我怎樣才能讓我的程式執行與第一次搜索相同的步驟,但對于其余的值,而不必每次都手動更改發送鍵?
任何幫助將不勝感激。
uj5u.com熱心網友回復:
你不熟悉for回圈嗎?只需遍歷每個搜索項。
此外,您可以在此處使用 Selenium,但很有可能您可以通過 api 獲取資料。但除非您共享網址/站點,否則不會知道。
a_list = ['BC-9700021-1', 'BC-9700024-1', 'BC-9700121-2']
order_list = []
order_info = {}
for eachId in a_list:
driver.get("website")
wait = WebDriverWait(driver, 20)
search_box = wait.until(EC.visibility_of_element_located((By.ID,"Search"))).send_keys(eachId)
driver.switch_to.default_content()
submit_box = wait.until(EC.visibility_of_element_located((By.ID,"Submit"))).click()
soup = BeautifulSoup(driver.page_source,'html.parser')
def correct_tag(tag):
return tag.name == "span" and tag.get_text(strip=True) in {
"Order Amount",
"Item Name",
"Date",
"Warehouse Number",
}
for t in soup1.find_all(correct_tag):
order_info[t.text] = t.find_next_sibling(text=True).strip()
order_list.append(order_info)
order_df1 = pd.DataFrame(order_list)
datatoexcel = pd.ExcelWriter('Order_sheet.xlsx')
order_df1.to_excel(datatoexcel)
datatoexcel.save()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/447298.html
