我有一个代码,从惠普网站检索有关交换机的信息。该脚本工作得很好,并将信息输出到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的新手,在其他地方我无法找到解决方案。
它不是脚本运行时的问题,而是使脚本运行30个额外的URL而不是1的问题。 –