Saya mencoba mengunduh data dari gsmarena. Contoh kode untuk mengunduh spesifikasi HTC one me adalah dari situs berikut "http://www.gsmarena.com/htc_one_me-7275.php" seperti yang disebutkan di bawah ini:
Data pada website diklasifikasikan dalam bentuk tabel dan baris tabel. Datanya berformat:
table header > td[@class='ttl'] > td[@class='nfo']
File item.py:
import scrapy
class gsmArenaDataItem(scrapy.Item):
phoneName = scrapy.Field()
phoneDetails = scrapy.Field()
pass
Berkas laba-laba:
from scrapy.selector import Selector
from scrapy import Spider
from gsmarena_data.items import gsmArenaDataItem
class testSpider(Spider):
name = "mobile_test"
allowed_domains = ["gsmarena.com"]
start_urls = ('http://www.gsmarena.com/htc_one_me-7275.php',)
def parse(self, response):
# extract whatever stuffs you want and yield items here
hxs = Selector(response)
phone = gsmArenaDataItem()
tableRows = hxs.css("div#specs-list table")
for tableRows in tableRows:
phone['phoneName'] = tableRows.xpath(".//th/text()").extract()[0]
for ttl in tableRows.xpath(".//td[@class='ttl']"):
ttl_value = " ".join(ttl.xpath(".//text()").extract())
nfo_value = " ".join(ttl.xpath("following-sibling::td[@class='nfo']//text()").extract())
colonSign = ": "
commaSign = ", "
seq = [ttl_value, colonSign, nfo_value, commaSign]
seq = seq.join(seq)
phone['phoneDetails'] = seq
yield phone
Namun, saya diblokir segera setelah saya mencoba memuat halaman di shell scrapy menggunakan:
"http://www.gsmarena.com/htc_one_me-7275.php"
Saya bahkan sudah mencoba menggunakan DOWNLOAD_DELAY = 3 di settings.py.
Mohon sarankan bagaimana saya harus melakukannya.