作为scrapy的新手,我无法弄清楚为什么这个spider不抓取网站上的数据来抓取数据。我已经通过stackoverflow搜索可能的答案,但我看到它没有得到充分解决。我试图从网站上刮掉一个小镇餐厅列表。我对网站的安全功能没有详细的了解。请问与XPath选择元素相关的问题是什么?蜘蛛运行良好,除非它不会刮擦任何东西。你能否建议为什么它不刮,以及如何解决问题。蜘蛛具有下面的代码:scrapy/Python抓取但不抓取数据
try:
from scrapy.spiders import Spider
from urllib.parse import urljoin
from scrapy.selector import Selector
from scrapy.http import Request
except ImportError:
print ("\nERROR IMPORTING THE NESSASARY LIBRARIES\n")
#scrapy.optional_features.remove('boto')
class YelpSpider(Spider):
name = 'yelp_spider'
allowed_domains=["yelp.com"]
headers=['venuename','services','address','phone','location']
def __init__(self):
self.start_urls = ['https://www.yelp.com/springfield-il-us']
def start_requests(self):
requests = []
for item in self.start_urls:
requests.append(Request(url=item, headers={'Referer':'http://www.google.com/'}))
return requests
def parse(self, response):
requests=[]
sel=Selector(response)
restaurants=sel.xpath('//*[@id="wrap"]/div[4]/div/div[1]/div/div[3]/div[1]/div[1]/h1')
items=[]
for restaurant in restaurants:
item=YelpRestaurantItem()
item['venuename']=sel.xpath('//*[@id="wrap"]/div[4]/div/div[1]/div/div[3]/div[1]/div[1]/h1')
item['services']=sel.xpath('//*[@id="wrap"]/div[4]/div/div[1]/div/div[3]/div[1]/div[2]/div[2]/span[2]/a[1]')
item['address']=sel.xpath('//*[@id="wrap"]/div[4]/div/div[1]/div/div[4]/div[1]/div/div[2]/ul/li[1]/div/strong/address')
item['phone']=sel.xpath('//*[@id="wrap"]/div[4]/div/div[1]/div/div[4]/div[1]/div/div[2]/ul/li[3]/span[3]')
item['location']=sel.xpath('//*[@id="dropperText_Mast"]')
item['url']=response.url
items.append(item)
yield item
我items.py具有下面的代码:
import scrapy
class YelpRestaurantItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
url=scrapy.Field()
venuename = scrapy.Field()
services = scrapy.Field()
address = scrapy.Field()
phone = scrapy.Field()
location=scrapy.Field()
我假设你有缩进问题,请更正问题中的代码。而且,您是否尝试过调试您的代码?也许在每一次“餐厅......”的迭代中印刷一些东西? – eLRuLL
你在想什么?蜘蛛搜索一个id属性是“包装”的东西,但是当我打开起始url时,我没有发现任何匹配的东西。 – Casper
@Casper,我试图抓住名字,服务,地址,电话,地点。我也应该说,这是我第一次使用xpath和scrapy。我刚刚复制了Chrome/Developer Tools中突出显示的餐厅的xpath。然而,我想要在这个小镇上的餐馆业务清单,名称,服务,地址,电话和位置。 – Kaleab