2017-05-02 58 views
-1

我有一個代碼,從惠普網站檢索有關交換機的信息。該腳本工作得很好,並將信息輸出到CSV文件中。但是,現在我需要通過30個不同的開關循環腳本。Python:試圖爲多個(類似)網站(類似)的數據刮

我有一個存儲在CSV文檔中的URL列表。這裏有一些例子。

https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J4813A 
https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J4903A 
https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9019B 
https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9022A 

在我的代碼,我「網址」綁定到這些鏈接,推動通過代碼來獲取我需要的信息之一。

這裏是我的全碼:

url = "https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx? 
ProductNumber=J9775A" 
r = requests.get(url) 
soup = BeautifulSoup(r.content, 'lxml') 
table = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"}) 
headers = [header.text for header in table.find_all('th')] 
rows = [] 

for row in table.find_all('tr', {'releasetype': 'Current_Releases'}): 
    item = [] 
    for val in row.find_all('td'): 
     item.append(val.text.encode('utf8').strip()) 
    rows.append(item) 

with open('c:\source\output_file.csv', 'w', newline='') as f: 
    writer = csv.writer(f) 
    writer.writerow({url}) 
    writer.writerow(headers) 
    writer.writerows(rows) 

我試圖找到自動化最好的辦法,因爲這個腳本需要至少每週運行一次。它需要輸出到每次被覆蓋的1個CSV文件。該CSV文件然後鏈接到我的Excel表作爲數據源

請在處理我的無知中找到耐心。我是Python的新手,在其他地方我無法找到解決方案。

回答

0

你在linux系統上嗎?您可以設置一個cron作業來隨時運行腳本。

+0

它不是腳本運行時的問題,而是使腳本運行30個額外的URL而不是1的問題。 –

0

就我個人而言,我只需製作每個「ProductNumber」唯一查詢參數值的數組,並通過循環遍歷該數組。

然後,通過調用其餘的代碼,因爲它將封裝在該循環中,您應該能夠完成此任務。

+0

感謝您的反饋 –