저는 Python (2 ~ 3 일)으로 코딩하는 것을 처음 접했고 기본적으로 stackoverflow에서 다른 사람들의 코드를 배우고 있습니다. 내가 쓰려고하는 코드는 craigslist에 오토바이의 PID와 해당 가격을 얻기 위해 beautifulsoup를 사용합니다. 나는이 일을 여러 가지 방법이 있습니다 알고 있지만 내 현재 코드는 다음과 같습니다 : 그것은 잘 작동beautifulsoup를 사용하여 craigslist에서 가격을 얻으십시오.
from bs4 import BeautifulSoup
from urllib2 import urlopen
u = ""
count = 0
while (count < 9):
site = "http://sfbay.craigslist.org/mca/" + str(u)
html = urlopen(site)
soup = BeautifulSoup(html)
postings = soup('p',{"class":"row"})
f = open("pid.txt", "a")
for post in postings:
x = post.getText()
y = post['data-pid']
prices = post.findAll("span", {"class":"itempp"})
if prices == "":
w = 0
else:
z = str(prices)
z = z[:-8]
w = z[24:]
filewrite = str(count) + " " + str(y) + " " +str(w) + '\n'
print y
print w
f.write(filewrite)
count = count + 1
index = 100 * count
print "index is" + str(index)
u = "index" + str(index) + ".html"
내가 계속 학습으로 나는 그것을 최적화 할 계획입니다. 지금 당장 가지고있는 문제는 가격이없는 항목이 여전히 나타나고 있다는 것입니다. 제가 실종되었다는 것이 명백합니다. 덕분에 .
감사합니다. 그러나 문제는 여전히 존재합니다. 파일을 쓰면 값이없는 행이 '0'대신 공백으로 표시됩니다. – Rudy