我使用scrapy來抓取justdial.com,但代碼似乎不起作用。請幫我解決這個問題。我使用命令「scrapy crawl justdial -o items.csv -t csv」從終端運行它。使用Scrapy抓取網頁數據
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from justdial_sample.items import JustdialSampleItem
class MySpider(CrawlSpider):
name = "justdial"
allowed_domains = ["www.justdial.com"]
start_urls = ["https://www.justdial.com/"]
rules = (Rule (SgmlLinkExtractor(allow=("index\d00\.html",
),restrict_xpaths=('//p[@id="nextpage"]',))
, callback="parse_items", follow=True),
)
def parse(self, response):
hxs = HtmlXPathSelector(response)
titles = hxs.select("//p")
items = []
for titles in titles:
item = JdItem()
item ["title"] = titles.select("a/text()").extract()
item ["link"] = titles.select("@/href").extract()
item.append(item)
return items
這是我使用的代碼。
AFAIK justdial阻止谷歌的任何其他刮刀。它不僅僅通過['robots.txt'](https://us.justdial.com/robots.txt)文件進行限制,而且它們不允許IP地址。您可以嘗試在scrapy中使用'USER_AGENT'字符串或嘗試使用scrapy splash。 –