我是Python的初学者,并且我一直在尝试一些项目。我有一个excel电子表格,其中包含我要打开的URL列,从中提取一些数据,输出到我的电子表格上的其他列,然后转到下一个URL并重复。
我可以编写代码,如果我输入一个URL,就可以完成几乎整个过程,但是我很讨厌创建循环,很难在URL列中逐行浏览,并使其重复该序列。
我的URL列表只有10个,但是我陷入了循环,已经创建了一个无限循环,导致我收到“ HTTP错误429:请求太多”。我以为我在一个空单元格值的中断处编写了代码,但是没有生效。
我的问题是,我可以使用什么代码使我可以进入URL列表,并从页面中提取所有数据,并将其输出到Excel工作表的另一列。
import urllib.request, csv, pandas as pd
from openpyxl import load_workbook
xl = pd.ExcelFile("filename.xlsx")
ws = xl.parse("Sheet1")
i = 0 # This is where I insert the row number for a specific URL
urlpage = str(ws['URLPage'][i]) # 'URLPage' is the name of the column in Excel
p = urlpage.replace(" ", "") # This line is for deleting whitespace in my URL
response = urllib.request.urlopen(p)
#The below lines of code are for pulling the data from the webpages
html = response.read()
website_text = html.decode('utf-8')
rating = (website_text.find('''Nqfqlb"''') + 8)
website_rating = website_text[(rating):(rating + 3)]
print(website_rating)
另外,如前所述,我是Python的新手,所以如果您看到我可以改善已有代码的地方,请告诉我。