1
私はプログラミングが新しく、私はPython BeautifulSoupですべてのページを掻き集めることに問題があります。私は最初のページを掻く方法を考え出しましたが、私はすべてのページを行う方法に迷っています。ページは2と番号が付けられているため、URL%dは高く評価される3などページ番号をpythonで掻き集める方法BeautifulSoup
すべての助けを借りて、1ページにすべての時間が長くなりますので
Here is the code:
#!/usr/bin/python
# -*- encoding: utf-8 -*-
from urllib2 import urlopen
import json
from BeautifulSoup import BeautifulSoup
defaultPage = 1
items = []
url = "https://www.nepremicnine.net/oglasi-prodaja/ljubljana-mesto/stanovanje/%d/"
def getWebsiteContent(page=defaultPage):
return urlopen(url % (page)).read()
def writeToFile(content):
file = open("nepremicnine1.json", "w+")
json.dump(content, file)
# file.write(content)
file.close()
def main():
content = getWebsiteContent(page=defaultPage)
soup = BeautifulSoup(content)
posesti = soup.findAll("div", {"itemprop": "itemListElement"})
for stanovanja in posesti:
item = {}
item["Naslov"] = stanovanja.find("span", attrs={"class": "title"}).string
item["Velikost"] = stanovanja.find("span", attrs={"class": "velikost"}).string
item["Cena"] = stanovanja.find("span", attrs={"class": "cena"}).string
item["Slika"] = stanovanja.find("img", src = True)["src"]
items.append(item)
writeToFile(items)
main()
は、だから私は、ループスルーしたいです。
パーフェクトに動作するはずです、助けてくれてありがとうあなたは、私は「私の神経:) – Jerry
の多くを保存:)と思いますが
ページをこする仕上げ
defaultPage
変数たびにインクリメントする必要があり私は助けてくれると嬉しかった=) –