我正在尝试制作一个简单的程序,该程序生成一个URL列表,然后可以从中进行抓取。我怀疑我这样做是最有效的方法,但我有这个能力。
是否可以在列表中放置方括号和单引号?
#reads in list of towns that I want to scrape
f = open('townList.csv', 'r')
reader = csv.reader(f)
towns= []
for row in reader:
towns.append(row)
#base url. the rest of the url follows the convention 'town-state-abbreviation'
base_url = "https://datausa.io/profile/geo/"
url_list = []
for n in range(len(towns)):
url = f'{base_url}{towns[n]}{"-pa"}'
url_list.append(url)
print(url_list)
with open('urls.csv', 'w') as output:
writer = csv.writer(output, lineterminator='\n')
for val in url_list:
writer.writerow([val])
我得到的是:
https://datausa.io/profile/geo/['Easton']-pa
我想得到:
https://datausa.io/profile/geo/Easton-pa
答案 0 :(得分:0)
简历阅读器是列表的迭代器。快速解决方法可能是:
for row in reader:
towns.append(row[0])
但这暗示您的文件不是csv文件,而是一个简单的连续文件,每行有一个on值。所以恕我直言,您想要的是:
#reads in list of towns that I want to scrape
with open('townList.csv', 'r') as f:
towns = [line.strip() for line in f]