0
다음 코드는 이름 및 주소 변수를 CSV 파일에 쓰지 못하고 있습니다. 숫자 나 단어 또는 "쓰기"변수를 사용하여 테스트하면 CSV에 기록되지만 "Writeaddress"와 "WriteName"은 *가되지 않습니다. (또한이 변수의 원래 소스를 사용하면 공백)Writerow가 csv 파일에 일부 변수를 인쇄하지 않습니다.
import requests, sys, pyperclip, bs4, csv
StationList = open('CTA Station Addresses.csv', 'w', newline='')
StationWrite = csv.writer(StationList)
for i in range(149):
id = str(i)
res = requests.get('http://www.transitchicago.com/travel_information /station.aspx?StopId=' + id)
res.raise_for_status()
Station = bs4.BeautifulSoup(res.text)
Name = Station.select('.rtehdng')
Address = Station.select('#ctl07_divAddress')
Write = 0
if Name == []:
print('missing name')
Write = 1
else:
#print(Name[0].getText())
WriteName = Name[0].getText()
pass
if Address == []:
print('missing address')
Write = 1
else:
#print(Address[0].getText())
WriteAddress = Address[0].getText()
pass
if Write == 0:
StationWrite.writerow([Write, WriteName, WriteAddress])
Write = 0
StationList.close()
* (내가 할 수있는 "writerows ([3, Writename 쓰기])과 CSV 행이 될 것"
주석 처리 된 인쇄 문과 주석 처리되지 않은 문구가 표시됩니다. 그거 뭐야? – zondo
예, 디버깅 추적 결과를 보여주세요. – Prune
URL에서 공백을 제거하는 등의 몇 가지 수정 작업을 통해 코드를 실행했으며 'WriteName' 및'WriteAddress'에 대한 값을 가져 왔습니다. 그들은 csv가 조금 별나게 보였을 정도로 개조를 포함했다. 그러나 그들은 거기에 있었다. 문제를 재현 할 수 없습니다. 이 스크립트를 실행 했습니까? – tdelaney